static NSString *orderCellID = @"MyOrderCell";
[self.mTableView registerClass:[MyOrderCell class] forCellReuseIdentifier:orderCellID];
1.MyOrderCell *cell = (MyOrderCell *)[tableView dequeueReusableCellWithIdentifier:orderCellID];
if (cell == nil) {
cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:orderCellID];
}
2.MyOrderCell *cell = [self.mTableView dequeueReusableCellWithIdentifier:orderCellID forIndexPath:indexPath];
这两种重用cell的方法问题就是...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell height %f",cell.frame.size.height);
return cell.frame.size.height;
}
如果用方法2.的话为什么UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
就报错了?????
但是用方法1的话确实是可行的...可以来个大神详细解释下吗
你的两种方法其实就是一种,是说,
这个方法,不论现在是否有已经生成的等待重用的cell,它都保证会返回一个cell(如果有的话就取,没有的话就再生成一个新的)。但是这个方法调用的时候有一个条件,就是你必须已经调用了registerClass或者registerNib方法注册过,这个文档里面都是写的有的。
而另一个方法则是判断是否有已经生成的等待重用的cell,有的话就取一个返回,没有的话就返回nil,所以需要你在下面判断是否为nil然后自己创建。
这个方法不能自己手动调用,这个方法返回的cell是要展示在界面上并且参与重用的,如果你手动调用了这个方法,它返回的cell在你算高之后就没用被释放掉了。你代码里面这里算高的时候每次调用方法都会生成一个cell。
所以你所纠结的重用的问题其实就是因为你手动调用了tableView:cellForRowAtIndexPath:这个函数。