美文网首页
UITableView 的相关使用

UITableView 的相关使用

作者: _凉风_ | 来源:发表于2016-06-23 18:00 被阅读41次

UITableView 继承自 UIScrollView,tableView 如何显示数据

  • 设置 dataSource 数据源
  • 数据源要遵守 UITableViewDataSource 协议
  • 数据源要实现协议中的某些方法
// 告诉tableView一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

// 告诉tableView第section组有多少行 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

// 告诉tableView第indexPath行显示怎样的cell 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

// 告诉tableView第section组的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

// 告诉tableView第section组的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

UITableView 的常见设置

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

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

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

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

1. Cell 的循环利用,在 Cell 数据源方法里设置标识符

当有一个cell进入视野范围内就会调用

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

    // 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";

    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    }

    // 3.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];

    return cell;
}

2. Cell 的循环利用,在加载控制器时设置标志符

  • 定义一个全局变量
// 定义重用标识
NSString *ID = @"cell";
  • 注册某个标识对应的 Cell 类型
// 在这个方法中注册cell
- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册某个标识对应的cell类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
  • 在数据源方法中返回 Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 1.去缓存池中查找cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];

    return cell;

}

3. Cell 的循环利用,在 Storyboard 设置标识符

  • 在 storyboard 中设置 UITableView 的 Dynamic Prototypes Cell
  • 设置 cell 的重用标识
  • 在代码中利用重用标识获取 cell
// 0.重用标识
// 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
static NSString *ID = @"cell";

// 1.先根据cell的标识去缓存池中查找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

// 2.覆盖数据
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];

错误:将 UIViewController 当做 UITableViewController 来用

4. 自定义 Cell

I. 等高的 Cell

1)Storyboard 自定义 Cell

1.创建一个继承自UITableViewCell的子类,比如DealCell
2.在 Storyboard 中

  • 往 Cell 里面增加需要用到的子控件
  • 设置 Cell 的重用标识
  • 设置 Cell 的 class 为 DealCell

3.在控制器中

  • 利用重用标识找到 Cell
  • 给 Cell 传递模型数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"deal";
    DealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 取出模型数据
    cell.deal = self.deals[indexPath.row];

    return cell;
}

4.在 DealCell 中

  • 将 Storyboard 中的子控件 连线 到类扩展中

@interface DealCell()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;

@end
  • 需要提供一个模型属性,重写模型的 set 方法,在这个方法中设置模型数据到子控件上
@class Deal

@interface DealCell: UITableViewCell
/** 模型数据 */

@property (nonatomic, strong) Deal *deal;

@end

- (void)setDeal:(Deal *)deal{
    _deal = deal;
    self.iconView.image = [UIImage imageNamed:deal.icon];
    self.titleLabel.text = deal.title;
    self.priceLabel.text = [NSString stringWithFormat:@"$%@", deal.price];
    self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];

}

2)xib 自定义 Cell

1.创建一个继承自 UITableViewCell 的子类,比如 DealCell
2.创建一个 xib 文件(文件名建议跟cell的类名一样),比如 DealCell.xib

  • 拖拽一个 UITableViewCell 出来
  • 修改 Cell 的 class 为 DealCell
  • 设置 Cell 的重用标识
  • 往 Cell 中添加需要用到的子控件

3.在控制器中

  • 利用 registerNib... 方法注册 xib 文件
  • 利用重用标识找到cell(如果没有注册 xib 文件,就需要手动去加载 xib 文件)
  • 给 Cell 传递模型数据

4.在 DealCell 中

  • 将 xib 中的子控件连线到类扩展中
  • 需要提供一个模型属性,重写模型的 set 方法,在这个方法中设置模型数据到子控件上
  • 也可以将创建获得 cell 的代码封装起来(比如 cellWithTableView: 方法)

3)代码自定义 Cell 「使用 frame」

1.创建一个继承自 UITableViewCell 的子类,比如 DealCell
initWithStyle: reuseIdentifier: 方法 中

  • 添加子控件
  • 设置子控件的初始化属性(比如文字颜色、字体)

layoutSubviews 方法中设置子控件的 frame
需要提供一个模型属性,重写模型的 set 方法,在这个方法中设置模型数据到子控件

2.在控制器中

  • 利用 registerClass... 方法注册 DealCell 类
  • 利用重用标识找到 Cell(如果没有注册类,就需要手动创建 Cell)
  • 给cell传递模型数据
  • 也可以将创建获得 Cell 的代码封装起来(比如 cellWithTableView: 方法)

4)代码自定义 Cell「使用 autolayout」

1.创建一个继承自 UITableViewCell 的子类,比如 DealCell
initWithStyle:reuseIdentifier: 方法中

  • 添加子控件
  • 添加子控件的约束(建议使用 Masonry
  • 设置子控件的初始化属性(比如文字颜色、字体)
    需要提供一个模型属性,重写模型的 set 方法,在这个方法中设置模型数据到子控件

2.在控制器中

  • 利用 registerClass... 方法注册 XMGDealCell 类
  • 利用重用标识找到 cell(如果没有注册类,就需要手动创建 Cell)
  • 给cell传递模型数据
  • 也可以将创建获得 cell 的代码封装起来(比如 cellWithTableView: 方法)

II. 非等高的 Cell

xib 自定义 Cell

  • 在模型中增加一个 cellHeight 属性,用来存放对应 cell 的高度
  • 在cell的模型属性 set 方法中调用 [self layoutIfNeed] 强制布局,然后计算出模型的 cellheight 属性值
  • 在控制器中实现 tableView:estimatedHeightForRowAtIndexPath: 方法,返回一个估计高度,比如 200
  • 在控制器中实现 tableView:heightForRowAtIndexPath: 方法,返回cell的真实高度(模型中的 cellHeight 属性)

5. 数据刷新

I. 数据刷新方法

  • 重新刷新屏幕上的所有 Cell
[self.tableView reloadData];
  • 刷新特定行的 Cell
[self.tableView reloadRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 插入特定行数的 Cell
[self.tableView insertRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 删除特定行数的 Cell
[self.tableView deleteRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];

II. 数据刷新的原则

通过修改模型数据,来修改 tableView 的展示

  • 先修改模型数据
  • 再调用数据刷新方法

注意:不要直接修改 Cell 上面子控件的属性

相关文章

网友评论

      本文标题:UITableView 的相关使用

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