美文网首页
2023-10-28 iOS 不用tableheader实现悬停

2023-10-28 iOS 不用tableheader实现悬停

作者: nickNic | 来源:发表于2023-10-27 09:45 被阅读0次
#import "SuspendTabTableViewCell.h"

#define screen_width [UIScreen mainScreen].bounds.size.width
#define screen_height [UIScreen mainScreen].bounds.size.height

#define RandomColor [UIColor colorWithHue:( arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5 brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:1]
#define TopMargin  100
#define HeaderPadding 20
#define HeaderHeight 200

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UILabel *topLB;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *headerIV;

@property (nonatomic, strong) SuspendTabTableViewCell *suspendCell;
@property (nonatomic, strong) UIView *floatView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self addSubView];
}

- (void)addSubView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    

    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, HeaderHeight)];
//    self.headerView.backgroundColor = [UIColor cyanColor];
    
    self.headerIV = [[UIImageView alloc]initWithFrame:self.headerView.bounds];
    [self.headerView addSubview:self.headerIV];
    self.headerIV.image = [UIImage imageNamed:@"header.jpg"];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = self.headerView;
    
    [self.tableView registerClass:[SuspendTabTableViewCell class] forCellReuseIdentifier:@"SuspendTabTableViewCell"];
    
    self.floatView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, screen_width, 40)];
    [self.view addSubview:self.floatView];
    self.floatView.backgroundColor = [UIColor cyanColor];
    self.floatView.hidden = YES;
//    self.floatView.alpha = 0;
    
}
- (UILabel *)creatLB
{
    UILabel *lb = [[UILabel alloc]init];
    [self.view addSubview:lb];
    lb.backgroundColor = RandomColor;
    lb.textAlignment = NSTextAlignmentCenter;
    lb.font = [UIFont systemFontOfSize:25];
    return lb;
}
#pragma mark - UITableViewDelegte
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 5) {
        static  NSString *  CellIdentifier = @"SuspendTabTableViewCell";
        SuspendTabTableViewCell  * cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
        self.suspendCell = cell;
        [cell setClickTabBlock:^(NSInteger index) {
            [self.tableView reloadData];
        }];
        return cell;
    } else {
        static  NSString *  CellIdentifier = @"CellIdentifier";
        UITableViewCell  * cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.backgroundColor = [UIColor whiteColor];
        
        cell.textLabel.text = [NSString stringWithFormat:@"cell位置:%ld",(long)indexPath.row];
        return cell;
    }
    return nil;
    
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    float ofsetY = scrollView.contentOffset.y;
    float limitY = self.headerView.frame.size.height + 40*5;
    if (ofsetY >= limitY) {
        [self.floatView addSubview:self.suspendCell.contentView];
        self.floatView.hidden = NO;
    } else {
        self.floatView.hidden = YES;
        [self.suspendCell addSubview:self.suspendCell.contentView];
    }
}
@end

相关文章

网友评论

      本文标题:2023-10-28 iOS 不用tableheader实现悬停

      本文链接:https://www.haomeiwen.com/subject/aiiuidtx.html