iOS UITableView

来源:互联网 发布:红警模型淘宝 编辑:程序博客网 时间:2024/05/20 05:29

UITableView 是 iOS 中用得相当多的一个控件。

  • 两个协议:UITableViewDataSource, UITableViewDelegate.

  • 每个分区称为一个 section, 每一行称为一个 row, 编号都是从 0 开始。

方法:

  • 设置多少区:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  • 设置多少行:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  • 设置表的内容:
// 常用如下设置:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (!cell) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];    }    // 配置内容    return cell;}
  • 设置区头:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
  • 设置区头的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
  • 表的点击事件:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  • 设置 Cell 的高度:
// 动态的设置每个cell的高度,适用于不等高的cell.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

注意:如果是等高的 cell, 可以设置 tableView.rowHeight 属性,是每行等高;若不设置,默认 cell 的高度 44.

  • 设置内容缩进(暂未用到,待验证):
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
  • 数组中返回右边的索引显示的内容(暂未用到,待验证):
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
  • 滑动选择的行后删除(暂未用到,待验证):
// 提交编辑。只要实现了这个方法,就可以实现左滑出现按钮。- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
  • 提交编辑出现的按钮(暂未用到,待验证):
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath; //数组中装的是UITableViewRowAction对象

参考:
1. IOS控件UITableView详解
2. UI第九天:UITableView简单介绍
3. UITableView

0 0