【轉(zhuǎn)】TableViewCell不重用

常規(guī)配置如下 當(dāng)超過tableView顯示的范圍的時候 后面顯示的內(nèi)容將會和前面重復(fù)
// 這樣配置的話超過頁面顯示的內(nèi)容會重復(fù)出現(xiàn)
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 定義唯一標(biāo)識 static NSString *CellIdentifier = @"Cell"; // 通過唯一標(biāo)識創(chuàng)建cell實例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判斷為空進(jìn)行初始化 --(當(dāng)拉動頁面顯示超過主頁面內(nèi)容的時候就會重用之前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 對cell 進(jìn)行簡單地數(shù)據(jù)配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell;}//通過以下3方案可以解決

方案一 取消cell的重用機(jī)制,通過indexPath來創(chuàng)建cell 將可以解決重復(fù)顯示問題 不過這樣做相對于大數(shù)據(jù)來說內(nèi)存就比較吃緊了
// 方案一 通過不讓他重用cell 來解決重復(fù)顯示- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 定義唯一標(biāo)識 static NSString *CellIdentifier = @"Cell"; // 通過indexPath創(chuàng)建cell實例 每一個cell都是單獨的 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // 判斷為空進(jìn)行初始化 --(當(dāng)拉動頁面顯示超過主頁面內(nèi)容的時候就會重用之前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 對cell 進(jìn)行簡單地數(shù)據(jù)配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell;}

方案二 讓每個cell都擁有一個對應(yīng)的標(biāo)識 這樣做也會讓cell無法重用 所以也就不會是重復(fù)顯示了 顯示內(nèi)容比較多時內(nèi)存占用也是比較多的和方案一類似
// 方案二 同樣通過不讓他重用cell 來解決重復(fù)顯示 不同的是每個cell對應(yīng)一個標(biāo)識- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 定義cell標(biāo)識 每個cell對應(yīng)一個自己的標(biāo)識 NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row]; // 通過不同標(biāo)識創(chuàng)建cell實例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判斷為空進(jìn)行初始化 --(當(dāng)拉動頁面顯示超過主頁面內(nèi)容的時候就會重用之前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 對cell 進(jìn)行簡單地數(shù)據(jù)配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell;}

方案三 只要最后一個顯示的cell內(nèi)容不為空,然后把它的子視圖全部刪除,等同于把這個cell單獨出來了 然后跟新數(shù)據(jù)就可以解決重復(fù)顯示
// 方案三 當(dāng)頁面拉動需要顯示新數(shù)據(jù)的時候,把最后一個cell進(jìn)行刪除 就有可以自定義cell 此方案即可避免重復(fù)顯示,又重用了cell相對內(nèi)存管理來說是最好的方案 前兩者相對比較消耗內(nèi)存- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 定義唯一標(biāo)識 static NSString *CellIdentifier = @"Cell"; // 通過唯一標(biāo)識創(chuàng)建cell實例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判斷為空進(jìn)行初始化 --(當(dāng)拉動頁面顯示超過主頁面內(nèi)容的時候就會重用之前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } else//當(dāng)頁面拉動的時候 當(dāng)cell存在并且最后一個存在 把它進(jìn)行刪除就出來一個獨特的cell我們在進(jìn)行數(shù)據(jù)配置即可避免 { while ([cell.contentView.subviews lastObject] != nil) { [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview]; } } // 對cell 進(jìn)行簡單地數(shù)據(jù)配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell;}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容