亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

關(guān)于UItabView Cell 自定義重用的 代碼

系統(tǒng) 1872 0

cocoa 默認(rèn)的cell風(fēng)格修改起來(lái)挺靈活的 先提供處自定義代碼 其實(shí)難點(diǎn)在于cell重用機(jī)制 供初學(xué)者參考

- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath

{

static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell" ;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier :showUserInfoCellIdentifier];

if (cell == nil )

{

// Create a cell to display an ingredient.

cell = [[[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleSubtitle

reuseIdentifier :showUserInfoCellIdentifier]

autorelease ];

UIImageView *leftico= [[[ UIImageView alloc ] init ] autorelease ];

leftico. tag = 11 ;

leftico. frame = CGRectMake ( 0 , 0 , 100 , 60 );

[leftico setContentMode : UIViewContentModeScaleAspectFit ];

UILabel *titles= [[[ UILabel alloc ] initWithFrame : CGRectMake ( 110 , 0 , 120 , 60 )] autorelease ];

[titles setBackgroundColor :[ UIColor clearColor ] ];

titles. tag = 22 ;

[cell addSubview :titles];

[cell addSubview :leftico];

cell. accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;//添加其默認(rèn)的細(xì)節(jié)按鈕

}

NSUInteger row=[indexPath row ];

NSLog ( @"name == %@" ,[ [ self . listdata objectAtIndex :row] objectForKey : @"name" ]);

UIImageView *imageView11 = ( UIImageView *)[cell viewWithTag : 11 ]; //重新指向那片內(nèi)存

//[ [cell viewWithTag:1] removeFromSuperview];

imageView11. image = [ UIImage imageNamed : @"gongshang.png" ];

UILabel *titles22= ( UILabel *)[cell viewWithTag : 22 ];

titles22. text =[ [ self . listdata objectAtIndex :row] objectForKey : @"name" ];

// if (cell.textLabel.text isEqualToString:@" 工商銀行 ") {

// cell.imageView.image= [ UIImage imageNamed:@"bg.jpg" ]

// }

return cell;

} 關(guān)于UItabView Cell 自定義重用的 代碼



下面有一網(wǎng)友做的例程 分析對(duì)比下 看看有什么收獲

解決自定義UITableViewCell在瀏覽中出現(xiàn)數(shù)據(jù)行重復(fù)的問(wèn)題
2010-12-27 10:52:22
原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時(shí)請(qǐng)務(wù)必以超鏈接形式標(biāo)明文章 原始出處 、作者信息和本聲明。否則將追究法律責(zé)任。 http://ddkangfu.blog.51cto.com/311989/465557

我在寫(xiě)一個(gè)App的時(shí)候自定義了一個(gè)UITableViewCell,但是這個(gè)UITableView在運(yùn)行的時(shí)候出現(xiàn)了每6行數(shù)據(jù)就循環(huán)重復(fù)顯示的問(wèn)題,而直接使用cell.textLabel.text顯示是沒(méi)有這個(gè)問(wèn)題,以下是我實(shí)現(xiàn)的代碼。

      
  1. -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. NSIntegersection=[indexPathsection];
  4. NSIntegerrow=[indexPathrow];
  5. UITableViewCell*cell;
  6. switch (section)
  7. {
  8. case 0:
  9. //dosomething.
  10. case 1:
  11. cell=[tableViewdequeueReusableCellWithIdentifier:@ "Cell" ];
  12. if (cell==nil)
  13. {
  14. cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@ "Cell" ]autorelease];
  15. //Image
  16. UIImageView*image=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0f,14.0f,45.0f,50.0f)];
  17. image.backgroundColor=[UIColorclearColor];
  18. image.image=[UIImageimageNamed:@ "folder.png" ];
  19. [cell.contentViewaddSubview:image];
  20. [imagerelease];
  21. //Label
  22. UILabel*titleLabel=[[UILabelalloc]initWithFrame:CGRectMake(45.0f,6.0f,214.0f,50.0f)];
  23. titleLabel.text=(NSString*)[(NSArray*)[self.categoryArrayobjectAtIndex:1]objectAtIndex:row];
  24. NSLog(@ "%@--%d" ,titleLabel.text,row);
  25. titleLabel.textAlignment=UITextAlignmentLeft;
  26. titleLabel.numberOfLines=3;
  27. titleLabel.tag=201;
  28. titleLabel.font=[UIFontboldSystemFontOfSize:14];
  29. [cell.contentViewaddSubview:titleLabel];
  30. [titleLabelrelease];
  31. }
  32. cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
  33. break ;
  34. }
  35. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  36. return cell;
  37. }

google了一下,目前已有的解決方案是將

      
  1. cell=[tableViewdequeueReusableCellWithIdentifier:@ "Cell" ];

替換成

      
  1. cell=[tableViewcellForRowAtIndexPath:indexPath];

      
  1. cell=nil;

這們做的目的去掉Cell的重用機(jī)制,但是這種方法都會(huì)在后臺(tái)隨著表格滾動(dòng)一直在創(chuàng)建cell,通過(guò)上面源代碼中Label定義里那句NSLog在控制臺(tái)輸出就可以看到,雖然會(huì)自動(dòng)回收內(nèi)存,但肯定也會(huì)給系統(tǒng)帶來(lái)不小開(kāi)銷,所以不到萬(wàn)一得以還是不會(huì)用的。

還有一種解決方案是自己定義Cell數(shù)組,在 tableView:tableView cellForRowAtIndexPath:中進(jìn)設(shè)置要顯示的cell,這是手工維護(hù)cell的一種方式,對(duì)大數(shù)據(jù)量的情況肯定是不適用的,不過(guò)也能算得上是一種思路吧,可以參考一下。其代碼如下:

      
  1. //在構(gòu)造函數(shù)里定義cell數(shù)組
  2. for ( int i=0;i<31;i++)
  3. {
  4. static NSString*MyBookMarkIdentifier=@ "CityMangerCell" ;
  5. cityCell[i]=[[CityMangerCellalloc]initWithFrame:CGRectZeroreuseIdentifier:MyBookMarkIdentifierinitIndex:i];
  6. }
  7. //使用它
  8. -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
  9. {
  10. if ((0<=indexPath.row)&&(indexPath.row<31))
  11. return cityCell[indexPath.row];
  12. return nil;
  13. }

后來(lái)我仔細(xì)分析了一下程序,找到了問(wèn)題所在:

原因是在if (cell == nil)判斷內(nèi)部不應(yīng)該對(duì)其label進(jìn)行賦值,即不使用這句:

      
  1. titleLabel.text =(NSString*)[(NSArray*)[self.categoryArrayobjectAtIndex:1]objectAtIndex:row];

正確的做法應(yīng)該是在if (cell == nil){}判斷后面進(jìn)行賦值。即

      
  1. if( cell ==nil)
  2. {
  3. ....
  4. }
  5. UILabel* l1 =(UILabel*)[cell.contentViewviewWithTag:201];
  6. l1.text =(NSString*)[(NSArray*)[self.categoryArrayobjectAtIndex:1]objectAtIndex:row];

分析原因如下:
UITableView中被實(shí)例化的cell個(gè)數(shù)由屏高和每個(gè)cell的高度決定,因?yàn)槲业腸ell高度設(shè)置為80,一屏只能 顯示6個(gè)Cell(只有6個(gè)cell被實(shí)例化),也就是只有這6個(gè)cell才會(huì)執(zhí)行if (cell == nil){}中的代碼,從第6行往后的cell都是重用的這6個(gè)cell,也就是說(shuō)從第7行開(kāi)始將不會(huì)執(zhí)行if (cell = nil){}中的代碼,當(dāng)UITableView需要繪制第7行cell的時(shí)候,會(huì)取得第1個(gè)cell進(jìn)行重用,如果我們不把原來(lái)第1行cell中的 Label內(nèi)容進(jìn)行修改,那么第7行將完全顯示第1行中的內(nèi)容,所以才會(huì)在第6行之后開(kāi)始出現(xiàn)數(shù)據(jù)重復(fù)的情況。
現(xiàn)在我將Label內(nèi)容設(shè)置的代碼放到if (cell == nil){}之后,它將會(huì)對(duì)每一個(gè)被重用的cell的Label進(jìn)行設(shè)定,也就不會(huì)再出現(xiàn)cell內(nèi)容重復(fù)的現(xiàn)象。
希望這個(gè)問(wèn)題的解決過(guò)程會(huì)對(duì)大家有所幫助。

本文出自 “ 一葉障目 ” 博客,請(qǐng)務(wù)必保留此出處 http://ddkangfu.blog.51cto.com/311989/465557


關(guān)于UItabView Cell 自定義重用的 代碼


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲综合色婷婷 | 中文字幕欧美一区 | 91精品欧美产品免费观看 | 91热久久免费频精品黑人99 | 亚洲精品成人一区二区aⅴ 亚洲精品成人一区二区www | 久久久久国产午夜 | 久久国产精品亚洲综合 | 久久成人在线观看 | 久久中文网 | 久久中文字幕日韩精品 | 亚洲国产人成中文幕一级二级 | 久久久噜久噜久久gif动图 | 欧美黑人激情性久久 | 久久精品伊人 | 91精品国产91 | 国产欧美日韩一区 | 人人澡 人人澡 人人看欧美 | 亚洲精品国产高清不卡在线 | 色婷婷视频 | 久久久精品久久久久久 | 黄色的网站在线观看 | 毛片1| 亚洲精品国自产拍影院 | www.亚洲天堂 | 99国产精品一区二区 | 国产精品美女久久久久 | 深夜福利免费观看 | 日韩一区二区精品久久高清 | 久久综合一区 | 久久夜色精品国产欧美 | 国产精品视频一区二区猎奇 | 男女免费在线视频 | 亚洲一区二区三区欧美 | 99久久99热精品免费观看国产 | 日本免费人成黄页网观看视频 | chinese在线播放91国内 | 91国内精品久久久久怡红院 | 国语性猛交xxxx乱大交 | 奇米在线视频观看 | 国产一区二区三区影院 | 日日摸夜夜爽 |