UITableView 的相关使用

2016-06-23  本文已影响41人  _凉风_

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

// 告诉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
- (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:@"testdata - %zd", indexPath.row];

    return cell;

}

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

// 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 中

3.在控制器中

- (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 中


@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
@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

3.在控制器中

4.在 DealCell 中

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

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

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

2.在控制器中

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

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

2.在控制器中

II. 非等高的 Cell

xib 自定义 Cell

5. 数据刷新

I. 数据刷新方法

[self.tableView reloadData];
[self.tableView reloadRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView insertRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView deleteRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];

II. 数据刷新的原则

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

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

上一篇 下一篇

猜你喜欢

热点阅读