UITableView 详解

1.registerNib和 registerClass: 区别

registerNib:很明显需要xib,cartcell.h  cartcell.m  carcell.xib

[_tableView registerNib [UINib nibWithNibName:@"xxxxxCell"bundle:nil] forCellReuseIdentifier:kCellIdentify];

xxxxxCell*cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];

如果为空,会自动从awakeFromNib生成。

registerClass:不需要xib,只需要cartcell.h  cartcell.m.


xxxxxCell*cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];

如果为空,会自动从initWithStyle生成。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self)

{

// cell页面布局

[self setupView];

}

return self;

}

2.最原始方案废弃

[tableView dequeueReusableCellWithIdentifier:kCellIdentify];//这个得判断是否为空。

[tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];//这个不需要,但是得注册。class 或者 nib。

判断是否重复创建 打印一下 NSLog("%@",cell)的地址。

cell 创建有if 一定要有else 成对处理。否则重用机制。

uitaTableViewCell 现在可以直接带xib。早期还得分两步创建。

3.uitabliew 优化

3.1提前计算高度

一个model 对应一个cell.再model增加一个Height属性,提前算好高度并缓存到model里面去。

cellforheight  计算一次  布局高   cellforindexpath 不仅计算布局 还得计算布局的位置。所有这两个地方有重复的地方。

再优化方案,是model 增加所有子控件的frame,这样就只需要计算一遍。


苹果通过调用每个cell,刚开始提前调用多次cellforheight 。就是为了确认contentsize。


4.以下滑动默认的相应方案 和  默认的按钮标题的修改方法

// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath;

这个修改删除标题的方法

- (nullableNSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPathNS_AVAILABLE_IOS(3_0)__TVOS_PROHIBITED;

5.出现更多按钮

通过[button superView ] superView]方法能得到myTabelviewCell

然后获得indexpath,不需要button.tag传值了

myTableViewCell * cell =(myTableViewCell *)[[deleteBtm superview] superview];NSIndexPath*indexPath = [self.mainTable  indexPathForCell:cell];  [self.mainTable beginUpdates];if([self.delegate respondsToSelector:NSSelectorFromString(@"deleteCellForindex:")]) {    [self.delegate deleteCellForindex:indexPath.row];}[self.mainTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];[self.mainTable  endUpdates];


6.

//self.tableView.allowsSelection=NO;//非编辑模式是否允许选中

//self.tableView.allowsMultipleSelection=YES;//非编辑模式

//self.tableView.allowsMultipleSelectionDuringEditing=YES;//编辑模式下可以多选

//self.tableView.allowsSelectionDuringEditing=YES;//编辑模式下单选

//[self.tableView setEditing:YES animated:YES];  //进入编辑状态

//self.tableView.isEditing //是否再编辑

你可能感兴趣的:(UITableView 详解)