/** * 何时调用:每当有一个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; }
// 定义重用标识 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; }
在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]; return cell;
// 分割线颜色 self.tableView.separatorColor = [UIColor redColor]; // 隐藏分割线 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // tableView有数据的时候才须要分割线 // 开发小技巧:快速取消分割线 self.tableView.tableFooterView = [[UIView alloc] init];
// 取消选中的样式(经常使用) 让当前 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
字体
storyboard自定义cell
优化
2.在storyboard中3d
往cell里面增长须要用到的子控件
code
设置cell的重用标识
orm
设置cell的class为XMGDealCell
blog
3.在控制器中继承
4.在XMGDealCell中
将storyboard中的子控件连线到类扩展中
须要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
xib自定义cell
代码自定义cell(使用frame)
代码自定义cell(使用autolayout)
Masonry
)非等高的cell
xib自定义cell
storyboard自定义cell
代码自定义cell(frame)
代码自定义cell(Autolayout)