美文网首页iOS日常知识储备iOS UI 基础iOS开发
基础好?UITableView的基础你知道多少?

基础好?UITableView的基础你知道多少?

作者: tanyufeng | 来源:发表于2016-06-02 19:06 被阅读1060次

    一、tableView的创建

    //利用大小和tableView样式创建tableView
      UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
      [self.view addSubview:tableView];
    
    //tableView 有两种样式Grouped和Plain, 他们的区别:
       /*
         1、默认情况下Grouped分区的高度比plain的分区高度要高
         2、使用Plain样式时,分区头和分区尾会停靠在顶部和底部
         3、使用Plain样式时,分区头和分区尾会有背景模糊效果
         */
    
    • 设置代理
    //数据源代理
    self.tableView.dataSource = self;
    //视图代理
    self.tableView.delegate = self;
    
    • 遵守协议
    @interface ViewController ()
    <UITableViewDataSource,UITableViewDelegate>
    @end
    

    二、tableView的常见设置

    // 设置每一行cell的高度
    self.tableView.rowHeight = 100;
    
    // 设置每一组头部的高度
    self.tableView.sectionHeaderHeight = 50;
    
    // 设置每一组尾部的高度
    self.tableView.sectionFooterHeight = 50;
    
    // 设置分割线颜色
    self.tableView.separatorColor = [UIColor redColor];
    
    // 设置分割线样式
    /*
    分割线样式主流为两种:
        UITableViewCellSeparatorStyleNone  :隐藏分割线
        UITableViewCellSeparatorStyleSingleLine  :显示分割线(默认)
    */
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置表头控件
    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    
    // 设置表尾控件
    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    
    // 设置右边索引文字的颜色
    self.tableView.sectionIndexColor = [UIColor redColor];
    
    // 设置右边索引文字的背景色
    self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];
    
    

    三、tableView 常用方法

    // 刷新整个tableView
     [tableView reloadData];
    
    // 获取cell所在的indexPath
    NSIndexPath *indexPath =[tableView indexPathForCell:cell];
    
    // 获取indexPath指定的cell视图
    cell =[tableView cellForRowAtIndexPath:indexPath];
    
    //获取section对应的头视图
    UIView *view =[tableView headerViewForSection:section];
    
    // 获取section对应的尾视图
    UIView *view = [tableView footerViewForSection:section];
    
    // 滚动到指定cell的对应为位置
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    

    三、数据源代理方法

    // 返回多少个分区
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    
    // 返回指定section分区有多少行数据
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    
    // 返回indexPath对应的单元格要显示的内容
    // indexPath包含分区号(section)和行号(row)两个属性
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    
    // 返回指定section分区的头部文本标题
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    
    // 返回指定section分区的尾部文本标题
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
    
    

    四、 tableViewCell的常见设置

    //主标题
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    //副标题
    cell.detailTextLabel.text = @"副标题";
    
    //左侧头像
    cell.imageView.image = [UIImage imageNamed:@"huoju_4.tiff"];
    
    
    // 设置右边的指示样式
    cell.accessoryType = [UITableView CellAccessoryDisclosureIndicator];
    
    // 设置右边的指示控件
    cell.accessoryView = [[UISwitch alloc] init];
    
    // 设置cell的选中样式
    cell.selectionStyle = [UITableView CellSelectionStyleNone];
    
    // backgroundView优先级 > backgroundColor
    // 设置背景色
    cell.backgroundColor = [UIColor redColor];
    
    // 设置背景view
    UIView *bg = [[UIView alloc] init];
    bg.backgroundColor = [UIColor blueColor];
    cell.backgroundView = bg;
    
    // 设置选中的背景view
    UIView *selectedBg = [[UIView alloc] init];
    selectedBg.backgroundColor = [UIColor purpleColor];
    cell.selectedBackgroundView = selectedBg;
    
    

    五、视图代理方法

    // 返回指定section的自定义分区头
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
    
    // 返回指定section的自定义分区尾
    - (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
    
    // 返回指定section的分区头高度
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
    
    // 返回指定section的分区尾高度
    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
    
    // 返回指定cell的高度
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    
    
    // 选中cell执行的方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    
    // cell将要出现时执行的方法
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
    
    // cell已经消失后执行的方法
    - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath;
    
    

    六、 cell的循环利用

    • 传统的写法
    /**
    *  每当有一个cell要进入视野范围内,就会调用一次
    */
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *ID = @"wine";
    // 1.先去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.如果缓存池中没有可循环利用的cell
    if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 3.设置数据
    cell.textLabel.text = [NSString stringWithFormat:@"%zd行的数据", indexPath.row];
    return cell;
    }
    
    • 新的写法(注册cell)
    NSString *ID = @"wine";
    - (void)viewDidLoad {
    [super viewDidLoad];
    // 注册某个重用标识 对应的 Cell类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 1.先去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.设置数据
    cell.textLabel.text = [NSString stringWithFormat:@"%zd行的数据", indexPath.row];
    return cell;
    }
    

    七、cell即将显示的动画效果

    //当一个cell即将显示的时候就会调用这个方法
    //通常这个方法,用来做cell的动画
    - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell
    forRowAtIndexPath:(NSIndexPath*)indexPath
    {
    cell.transform=CGAffineTransformMakeTranslation(self.view.width,0);
    [UIViewanimateWithDuration:0.5animations:^{
        cell.transform=CGAffineTransformIdentity;
    }];
    }
    

    相关文章

      网友评论

        本文标题:基础好?UITableView的基础你知道多少?

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