UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法
UITableView繼承自UIScrollview,是蘋果為我們封裝好的一個(gè)基于scroll的控件。上面主要是一個(gè)個(gè)的UITableViewCell,可以讓UITableViewCell響應(yīng)一些點(diǎn)擊事件,也可以在UITableViewCell中加入U(xiǎn)ITextField或者UITextView等子視圖,使得可以在cell上進(jìn)行文字編輯。
UITableView中的cell可以有很多,一般會(huì)通過重用cell來達(dá)到節(jié)省內(nèi)存的目的:通過為每個(gè)cell指定一個(gè)重用標(biāo)識(shí)符(reuseIdentifier),即指定了單元格的種類,當(dāng)cell滾出屏幕時(shí),會(huì)將滾出屏幕的單元格放入重用的queue中,當(dāng)某個(gè)未在屏幕上的單元格要顯示的時(shí)候,就從這個(gè)queue中取出單元格進(jìn)行重用。
但對(duì)于多變的自定義cell,有時(shí)這種重用機(jī)制會(huì)出錯(cuò)。比如,當(dāng)一個(gè)cell含有一個(gè)UITextField的子類并被放在重用queue中以待重用,這時(shí)如果一個(gè)未包含任何子視圖的cell要顯示在屏幕上,就會(huì)取出并使用這個(gè)重用的cell顯示在無任何子視圖的cell中,這時(shí)候就會(huì)出錯(cuò)。
解決方法:
方法1 將獲得cell的方法從- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 換為-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
重用機(jī)制調(diào)用的就是dequeueReusableCellWithIdentifier這個(gè)方法,方法的意思就是“出列可重用的cell”,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell),就可以不使用重用機(jī)制,因而問題就可以得到解決,雖然可能會(huì)浪費(fèi)一些空間。
示例代碼:
[plain] - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改為以下的方法 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根據(jù)indexPath準(zhǔn)確地取出一行,而不是從cell重用隊(duì)列中取出 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代碼 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改為以下的方法 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根據(jù)indexPath準(zhǔn)確地取出一行,而不是從cell重用隊(duì)列中取出 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代碼 }
方法2 通過為每個(gè)cell指定不同的重用標(biāo)識(shí)符(reuseIdentifier)來解決。
重用機(jī)制是根據(jù)相同的標(biāo)識(shí)符來重用cell的,標(biāo)識(shí)符不同的cell不能彼此重用。于是我們將每個(gè)cell的標(biāo)識(shí)符都設(shè)置為不同,就可以避免不同cell重用的問題了。
示例代碼:
[plain] - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]]; //以indexPath來唯一確定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代碼 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]]; //以indexPath來唯一確定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代碼 }
方法3 刪除重用cell的所有子視圖
這個(gè)方法是通過刪除重用的cell的所有子視圖,從而得到一個(gè)沒有特殊格式的cell,供其他cell重用。
示例代碼:
[plain] - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } else { //刪除cell的所有子視圖 while ([cell.contentView.subviews lastObject] != nil) { [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; } } //...其他代碼 }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS中UITableView Cell實(shí)現(xiàn)自定義單選功能
- IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理實(shí)例
- 詳解ios中自定義cell,自定義UITableViewCell
- IOS UITableView和UITableViewCell的幾種樣式詳細(xì)介紹
- iOS App開發(fā)中使用及自定義UITableViewCell的教程
- 詳解iOS開發(fā)中UITableview cell 頂部空白的多種設(shè)置方法
- 全面解析iOS應(yīng)用中自定義UITableViewCell的方法
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- iOS中使用UItableviewcell實(shí)現(xiàn)團(tuán)購和微博界面的示例
相關(guān)文章
iOS如何獲取設(shè)備型號(hào)的最新方法總結(jié)
在開發(fā)中,我們經(jīng)常需要獲取設(shè)備的型號(hào)以進(jìn)行數(shù)據(jù)統(tǒng)計(jì)或者做不同的適配。這篇文章主要給大家介紹了關(guān)于iOS如何獲取設(shè)備型號(hào)的最新方法,需要的朋友可以參考下2018-11-11iOS 獲取當(dāng)前的ViewController的方法
本篇文章主要介紹了iOS 獲取當(dāng)前的ViewController的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解
這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07iOS內(nèi)存錯(cuò)誤EXC_BAD_ACCESS的解決方法
iOS開發(fā),最郁悶的莫過于程序毫無征兆地就崩潰了,用bt命令打出調(diào)用棧,給出的是一堆系統(tǒng)EXC_BAD_ACCESS的信息,根本沒辦法定位問題出現(xiàn)在哪里2013-06-06Objective-C學(xué)習(xí)之ARC的實(shí)現(xiàn)方法
自動(dòng)引用計(jì)數(shù)(Automatic Reference Counting, ARC)把壓在程序員們肩頭的管理內(nèi)存的重?fù)?dān)卸除了不少,更不用說讓跟蹤內(nèi)存泄漏那樣的煩心事也少了很多。下面這篇文章主要給大家介紹了關(guān)于Objective-C學(xué)習(xí)之ARC的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒下。2017-12-12