美文网首页
UITableView

UITableView

作者: 木_木27 | 来源:发表于2015-07-24 19:54 被阅读171次

    --

    注意点:

    • cell的左边样式:
      • accessoryView的优先级大于accessoryType
      • backgroundView优先级大于backgroundColor

    相关属性:

    // 设置每一行cell的高度
    self.tableView.rowHeight = 100;
    // 设置每一组头部的高度
    self.tableView.sectionHeaderHeight = 50;
    // 设置每一组尾部的高度
    self.tableView.sectionFooterHeight = 50;
    // 设置分割线颜色
    self.tableView.separatorColor = [UIColor redColor];
    // 设置分割线样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置表头控件
    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    // 设置表尾控件
    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    // 设置cell的选中样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 设置背景色
    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;
    

    右侧索引条

    // 设置右边索引文字的颜色
    self.tableView.sectionIndexColor = [UIColor redColor];
    // 设置右边索引文字的背景色
    self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];
    //返回每一组的索引标题(数组中放的是字符串)
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        return [self.carGroups valueForKeyPath:@"title"];
    }
    

    UITableViewCell的contentView

    contentView下默认有3个子视图
    其中2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)
    第3个是UIImageView(通过UITableViewCell的imageView属性访问)

    UITableViewCellStyle属性

    Style

    UITableViewCell结构

    结构

    通过代码自定义cell(cell的高度不一致):

    1.新建一个继承自UITableViewCell的类
    2.重写initWithStyle:reuseIdentifier:方法
    添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加到contentView中)
    进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)
    3.提供2个模型
    数据模型: 存放文字数据\图片数据
    frame模型: 存放数据模型\所有子控件的frame\cell的高度
    4.cell拥有一个frame模型(不要直接拥有数据模型)
    5.重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame
    6.frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一次)

    如何让tableView展示数据

    • 设置数据源对象
    self.tableView.dataSource = self;
    
    • 数据源对象要遵守协议
    @interface ViewController () <UITableViewDataSource>
    @end
    
    • 实现数据源方法
    // 多少组数据
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    // 每一组有多少行数据
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    // 每一行显示什么内容
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    // 每一组的头部
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    // 每一组的尾部
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    

    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;
    }
    

    相关文章

      网友评论

          本文标题:UITableView

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