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