借鉴很多文章,但并没侵权
在创建时
1、UITableViewCell *cell = nil;
2、删除
2.1、UITableViewCell *cell = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
else{
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; //删除并进行重新分配
}
}
另一种方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdetify = @"cell";
UITableViewCell *tvCell = [tableView dequeueReusableCellWithIdentifier:cellIdetify];
if(tvCell == nil)
{
NSLog(@"cell = nil");
tvCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetify] autorelease];
}else{
NSLog(@"cell !=nil ");
NSArray *views = [tvCell subviews];
for (UIView *obj in views) {
if (obj.tag==1000 || obj.tag==2000) { //只删除指定的画面,不要全部删除,否则tableview的分割线也会被删除
NSLog(@"cell 要删除的子画面是:%@",[obj class]);
[obj removeFromSuperview];
}
}
3、标识符:NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
4、 static NSString *cellId = @"couponviewController";
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
本文详细介绍了UITableView中UITableViewCell的重用机制实现方式。通过不同场景下的代码示例,包括单元格初始化、子视图的清理及重用标识符的设置等,帮助读者深入理解UITableViewCell如何高效复用以提高应用性能。
7173

被折叠的 条评论
为什么被折叠?



