UITableViewCell 重合問題解決方法

這兩天作ios遇到一個UITableViewCell 數據重合的問題,緣由:引發這個問題的主要緣由是,重用cell。以前cell上的內容未被清空,而又增長新增內容所致。從網上查了一些解決方法,好比:ios

解決方案:在使用cell時,首先刪除cell上的view,代碼以下。ide

 

static NSString *identifier = @"Fanmeli";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
//cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}else{
// 刪除cell中的子對象,刷新覆蓋問題。
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
}
}

spa

 

除了以上方法還有就是不讓cell去複用,固然這種方式儘可能避免,上面的方案試了一下沒有起做用,不過也給我我啓發,既然是cell複用,內容沒有清空,那就再清空一下再賦值對象

 if (cell == nil) {
        cell = [[SystemNoticeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier];
    }else{
        [cell clearCell ];
    }

成功解決這種問題。blog

相關文章
相關標籤/搜索