国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法

2020-07-26 02:46:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

UITableView繼承自UIScrollview,是蘋(píng)果為我們封裝好的一個(gè)基于scroll的控件。上面主要是一個(gè)個(gè)的UITableViewCell,可以讓UITableViewCell響應(yīng)一些點(diǎn)擊事件,也可以在UITableViewCell中加入U(xiǎn)ITextField或者UITextView等子視圖,使得可以在cell上進(jìn)行文字編輯。

UITableView中的cell可以有很多,一般會(huì)通過(guò)重用cell來(lái)達(dá)到節(jié)省內(nèi)存的目的:通過(guò)為每個(gè)cell指定一個(gè)重用標(biāo)識(shí)符(reuseIdentifier),即指定了單元格的種類(lèi),當(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的子類(lèi)并被放在重用queue中以待重用,這時(shí)如果一個(gè)未包含任何子視圖的cell要顯示在屏幕上,就會(huì)取出并使用這個(gè)重用的cell顯示在無(wú)任何子視圖的cell中,這時(shí)候就會(huì)出錯(cuò)。

解決方法:

方法1 將獲得cell的方法從- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 換為-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

重用機(jī)制調(diào)用的就是dequeueReusableCellWithIdentifier這個(gè)方法,方法的意思就是“出列可重用的cell”,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell),就可以不使用重用機(jī)制,因而問(wèn)題就可以得到解決,雖然可能會(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 通過(guò)為每個(gè)cell指定不同的重用標(biāo)識(shí)符(reuseIdentifier)來(lái)解決。
重用機(jī)制是根據(jù)相同的標(biāo)識(shí)符來(lái)重用cell的,標(biāo)識(shí)符不同的cell不能彼此重用。于是我們將每個(gè)cell的標(biāo)識(shí)符都設(shè)置為不同,就可以避免不同cell重用的問(wèn)題了。

示例代碼:

[plain]- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];  //以indexPath來(lái)唯一確定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來(lái)唯一確定cell  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell  if (cell == nil) {    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  }  //...其他代碼}

方法3 刪除重用cell的所有子視圖

這個(gè)方法是通過(guò)刪除重用的cell的所有子視圖,從而得到一個(gè)沒(méi)有特殊格式的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];    }  }  //...其他代碼}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄢陵县| 宁德市| 闻喜县| 顺义区| 宁波市| 棋牌| 大英县| 阳江市| 永年县| 剑阁县| 鄂尔多斯市| 吕梁市| 陆良县| 米泉市| 稷山县| 嘉荫县| 托克托县| 泰宁县| 锡林郭勒盟| 海林市| 平利县| 昂仁县| 辰溪县| 洮南市| 玉溪市| 原阳县| 蓬莱市| 镇沅| 资源县| 大英县| 中西区| 东乡县| 布尔津县| 桑植县| 罗城| 龙井市| 滦平县| 台北市| 株洲市| 许昌县| 昂仁县|