UITableView使用总结

作者: 郑州程序员王一 | 来源:发表于2016-08-27 14:33 被阅读87次

    一:tableView知识点

    //设置分割线的样式为无
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
              
    //设置分割线的颜色
    self.tableView.separatorColor = [UIColor redColor];
         
    //设置分割线的上下左右,受影响的只有左右,
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
         
    //设置内边距
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
         
    //有内容的有下划线,没内容的无下划线
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    // 当选中某行的时候触发
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
         //立刻让当前行变为非选中
         [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    //设定tableView具体某一行的高度,或者整体的每一行高度
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
         if (indexPath.row % 2 == 0) {
             return 100;
         }else{
             return 55;
         }
    }
    
    //iOS8之后的自适应高度,(cell的垂直布局上下要记得设置)
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
         
    

    二:cell的总结

    //cell的frame从新布局  设置cell的尺寸,一般用来设置cell之间的分割线
     -(void)setFrame:(CGRect)frame{
         CGFloat margin = 10;
         frame.origin.x = margin;
         frame.size.width -= 2 * margin;
         frame.size.height -= margin;
         frame.origin.y += margin;
         [super setFrame:frame];
    }
    
    //初始化控件创建
    -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
          //创建控件
          self.selectionStyle = UITableViewCellSelectionStyleNone;
          self.contentView.backgroundColor = [UIColor whiteColor];
          self.backgroundColor = [UIColor whiteColor];
          self.layer.shouldRasterize = YES;
          self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
        }
        return self;
    }
    
    //添加系统箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //设置cell的颜色
    self.contentView.backgroundColor =  [UIColor redColor];
    
    //让用户点击cell不变色
     cell.selectionStyle =UITableViewCellSelectionStyleNone;
         
    //设置每个cell的背景图片
    cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_apple_small"]];
         
    //设置点击cell时的颜色
    UIView *clickView = [[UIView alloc]init];
    clickView.backgroundColor = [UIColor colorWithRed:191.0/255.0 green:219.0/255.0 blue:229.0/255.0 alpha:0.3];
    cell.selectedBackgroundView = clickView;
    

    三:tableView的简单使用

    3-1:声明重用标示符

    //重用标示符
    static NSString * const CellID = @"CellID";
    

    3-2:注册cell
    此处不考虑xib,storyboard,作者风格偏纯代码

    //注册cell
    [self.tableView registerClass:[WYCell class] forCellReuseIdentifier:CellID];
    

    3-3:使用

    //有多少个section
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    //section里有多少个cell
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.topics.count;
    }
    //赋值
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        RBCheckRegistrationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
        cell.invitationActivityModel = self.topics[indexPath.row];
        return cell;
    }
    

    四:UITaleView的Header和Footer使用,

    4-0:为每个section的Header或者Footer添加标题,实现以下2个方法即可,不再需要其他的操作。

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
    

    4-1:如果你不满意仅有标题的Header和Footer,可以为Header和Footer自定义View。
    自定义Header的View,实现UITableViewDelegate的:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UIView *headView = [[UIView alloc] init];
        headView.backgroundColor = WYRGBColor(214, 87, 76);
        self.tableView.tableHeaderView = headView;
    
        //时间
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.text = @"时间";
        timeLabel.textAlignment = NSTextAlignmentCenter;
        timeLabel.font = DEF_SFont15;
        timeLabel.textColor = [UIColor whiteColor];
        [timeLabel sizeToFit];
    
        //添加控件
        [headView addSubview:timeLabel];
    
       //随意设置下位置
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(headView);
            make.left.equalTo(headView.mas_left).offset(padding);
        }];
        return headerView;
    }
    

    除了实现UITableViewDelegate的方法返回自定义View之外,必须实现UITableViewDelegate的方法,明确返回Header的高,才能使自定义的View显示出来。

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
    

    4-2:自定义Footer的View,和上面实现的header一样,方法如下

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        UIView *footerView ...
        return footerView;
    }
    

    明确返回Footer的高,才能使自定义的View显示

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
    
    5:删除cell
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return @"删除";
    }
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 从数据源中删除
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 从列表中删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    

    五:tableView头部视图拉伸放大

    头部拉伸.gif

    1、三个不可缺少的属性

    //头部View
    @property(nonatomic,strong)UIView *headView;
    //头部图片
    @property(nonatomic,strong)UIImageView *headImageView;
    //状态栏注意事项
    @property(nonatomic,assign)UIStatusBarStyle statStyle;
    //图片高度   
    #define kHeadViewHeight 200
    

    2、实现的关键方法

    //创建tableView
    -(void)initTableView{
        self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellID];
        //设置tableView间距
        self.tableView.contentInset = UIEdgeInsetsMake(kHeadViewHeight, 0, 0, 0);
        self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(kHeadViewHeight, 0, 0, 0);
        [self.view addSubview:self.tableView];
    }
    
    -(void)initHeadView{
        //状态栏初始化
        _statStyle = UIStatusBarStyleLightContent;
        //头部视图
        self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYScreenW, kHeadViewHeight)];
        self.headView.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:self.headView];
        //头部图片
        self.headImageView = [[UIImageView alloc]initWithFrame:self.headView.bounds];
        self.headImageView.image = [UIImage imageNamed:@"headimage.jpg"];
        self.headImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.headImageView.clipsToBounds = YES;//图片过大的裁切
        [self.headView addSubview:self.headImageView];
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGFloat offsety = scrollView.contentOffset.y+scrollView.contentInset.top;
        NSLog(@"%f",offsety);
        if (offsety <= 0)
        {
            //放大
            CGRect frame= self.headView.frame;
            frame.origin.y = 0;
            
            frame.size.height = kHeadViewHeight - offsety;
            self.headView.frame = frame;
            
            frame = self.headImageView.frame;
            frame.size.height = self.headView.frame.size.height;
            self.headImageView.frame = frame;
    
            //导航栏的设置
            //设置透明度
            self.headImageView.alpha = 1.0;
            //根据透明度修改状态栏颜色
            _statStyle = UIStatusBarStyleLightContent;
            //主动更新状态栏
            [self.navigationController setNeedsStatusBarAppearanceUpdate];
            
        }else{
            //整体移动
            CGRect frame= self.headView.frame;
            //header的最小y值
    //        CGFloat min = kHeadViewHeight - 64;
            CGFloat min = kHeadViewHeight;
            frame.origin.y = -MIN(min, offsety);
            
            frame.size.height = kHeadViewHeight;
            self.headView.frame = frame;
            
            frame = self.headImageView.frame;
            frame.size.height = self.headView.frame.size.height;
            self.headImageView.frame = frame;
    
            //导航栏的设置
            CGFloat progress = 1-(offsety/min);
            self.headImageView.alpha = progress;
            //根据透明度修改状态栏颜色
            _statStyle = (progress < 0.5) ? UIStatusBarStyleDefault:UIStatusBarStyleLightContent;
            //主动更新状态栏
            [self.navigationController setNeedsStatusBarAppearanceUpdate];
        }
    }
    
    //状态栏方法
    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return UIStatusBarStyleLightContent;
    }
    

    导航栏

    勤学如早春之苗,不见其增,日有所涨。
    辍学如磨刀之石,不见其减,日有所损。

    相关文章

      网友评论

        本文标题:UITableView使用总结

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