UITableView

一、UITableView的数据源协议中的方法

1.告诉tableView一共有多少组数据

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

2.告诉tableView第section组有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

3.告诉tableView第indexPath行显示怎样的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

4.告诉tableView第section组的头部标题

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

5.告诉tableView第section组的尾部标题

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

二、UITableView的代理方法

1.选中某一行的时候调用(点击某一行)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

2.取消选中某一行的时候调用

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

3.告诉tableView第indexPath行cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

4.告诉tableView第section显示头部控件的高度

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

5.告诉tableView第section显示怎样的头部控件

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

6.返回每一行的估计高度
只要返回了估计高度,那么就会先调用tableView:cellForRowAtIndexPath:方法创建cell,再调用tableView:heightForRowAtIndexPath:方法获取cell的真实高度

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

7.这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

8.只要实现这个方法,左划cell出现删除按钮的功能就有了, 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {  // 点击了“删除”
        // 删除模型
        [self.deals removeObjectAtIndex:indexPath.row];
        
        // 刷新表格
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+
        NSLog(@"+++++ %zd", indexPath.row);
    }
}

三、UITableView的常见设置

// 分割线颜色
self.tableView.separatorColor = [UIColor redColor];

// 隐藏分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// tableView有数据的时候才需要分割线
// 开发小技巧:快速取消分割线
self.tableView.tableFooterView = [[UIView alloc] init];

// 允许在编辑模式进行多选操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;

四、UITableViewCell的常见设置

// 取消选中的样式(常用) 让当前 cell 按下无反应
cell.selectionStyle = UITableViewCellSelectionStyleNone;

// 设置选中的背景色
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;

// 设置默认的背景色
cell.backgroundColor = [UIColor blueColor];

// 设置默认的背景色
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView = backgroundView;

// backgroundView的优先级 > backgroundColor
// 设置指示器
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[UISwitch alloc] init];

五、自定义cell

1.storyboard自定义cell

 1)创建一个继承自UITableViewCell的子类,比如XMGDealCell
 2) 在storyboard中
        (1)往cell里面增加需要用到的子控件
        (2)设置cell的重用标识
        (3)设置cell的class为自定义Cell
 3) 在控制器中
        (1)利用重用标识找到cell
        (2)给cell传递模型数据
 4)在自定义Cell中
        (1)将storyboard中的子控件连线到类扩展中
        (2)需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上

2.xib自定义cell

1)创建一个继承自UITableViewCell的子类
2)创建一个xib文件(文件名建议跟cell的类名一样)
      拖拽一个UITableViewCell出来
      修改cell的class为XMGDealCell
      设置cell的重用标识
      往cell中添加需要用到的子控件
3)在控制器中
    利用registerNib...方法注册xib文件
    利用重用标识找到cell(如果没有注册xib文件,就需要手动去加载xib文件)
    给cell传递模型数据
4)在自定义Cell中
    将xib中的子控件连线到类扩展中
    需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
     也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)

3.代码自定义cell(使用frame)

1.创建一个继承自UITableViewCell的子类,比如XMGDealCell
    在initWithStyle:reuseIdentifier:方法中添加子控件
    设置子控件的初始化属性(比如文字颜色、字体)
    在layoutSubviews方法中设置子控件的frame
    需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
2.在控制器中
    利用registerClass...方法注册自定义Cell类
    利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
给cell传递模型数据
    也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)

4.代码自定义cell(使用autolayout)

1.创建一个继承自UITableViewCell的子类
    在initWithStyle:reuseIdentifier:方法中添加子控件
    添加子控件的约束(建议使用Masonry)
    设置子控件的初始化属性(比如文字颜色、字体)
    需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
2.在控制器中
    利用registerClass...方法注册自定义Cell类
    利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
    给cell传递模型数据
    也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容