借鉴很多文章,但并没侵权
在创建时
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];