欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

關于UItabView Cell 自定義重用的 代碼

系統 1954 0

cocoa 默認的cell風格修改起來挺靈活的 先提供處自定義代碼 其實難點在于cell重用機制 供初學者參考

- ( 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 ;//添加其默認的細節按鈕

}

NSUInteger row=[indexPath row ];

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

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

//[ [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;

} 關于UItabView Cell 自定義重用的 代碼



下面有一網友做的例程 分析對比下 看看有什么收獲

解決自定義UITableViewCell在瀏覽中出現數據行重復的問題
2010-12-27 10:52:22
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。 http://ddkangfu.blog.51cto.com/311989/465557

我在寫一個App的時候自定義了一個UITableViewCell,但是這個UITableView在運行的時候出現了每6行數據就循環重復顯示的問題,而直接使用cell.textLabel.text顯示是沒有這個問題,以下是我實現的代碼。

      
  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的重用機制,但是這種方法都會在后臺隨著表格滾動一直在創建cell,通過上面源代碼中Label定義里那句NSLog在控制臺輸出就可以看到,雖然會自動回收內存,但肯定也會給系統帶來不小開銷,所以不到萬一得以還是不會用的。

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

      
  1. //在構造函數里定義cell數組
  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. }

后來我仔細分析了一下程序,找到了問題所在:

原因是在if (cell == nil)判斷內部不應該對其label進行賦值,即不使用這句:

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

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

      
  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中被實例化的cell個數由屏高和每個cell的高度決定,因為我的cell高度設置為80,一屏只能 顯示6個Cell(只有6個cell被實例化),也就是只有這6個cell才會執行if (cell == nil){}中的代碼,從第6行往后的cell都是重用的這6個cell,也就是說從第7行開始將不會執行if (cell = nil){}中的代碼,當UITableView需要繪制第7行cell的時候,會取得第1個cell進行重用,如果我們不把原來第1行cell中的 Label內容進行修改,那么第7行將完全顯示第1行中的內容,所以才會在第6行之后開始出現數據重復的情況。
現在我將Label內容設置的代碼放到if (cell == nil){}之后,它將會對每一個被重用的cell的Label進行設定,也就不會再出現cell內容重復的現象。
希望這個問題的解決過程會對大家有所幫助。

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


關于UItabView Cell 自定義重用的 代碼


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 三级网站日本 | 国产欧美一区二区三区久久人妖 | 成人黄色小视频网站 | 亚洲欧美国产视频 | 中文字幕精品一区 | 午夜日韩精品 | 国产一区二区三区在线电影 | 五月色电影 | 久久精品网 | 中文字幕日韩欧美一区二区三区 | 久久综合玖玖爱中文字幕 | 日韩欧美视频一区二区三区 | 久久伊99综合婷婷久久伊 | 成人免费看黄网站yyy456 | 夜本色 | 骚视频在线观看 | 成人免费毛片aaaaaa片 | 亚洲精品视频在线 | 欧美一区二三区 | 成人亚洲视频在线观看 | 久草在线精品视频 | 免费欧美黄色网址 | 十六以下岁女子毛片免费 | 日韩a级片 | 一区二区免费视频观看 | 亚洲国产高清高潮精品美女 | 久久亚洲AV成人无码电影A片 | 国产成人免费永久播放视频平台 | 成人午夜电影在线观看 | 欧美综合国产 | 亚洲韩国精品 | 国产精品日韩在线观看 | 91国内在线视频 | 日朝欧美亚洲精品 | 99这里只有精品66视频 | 亚洲一区二区三区久久 | 久久久久国产精品免费免费搜索 | 国内自拍视频在线看免费观看 | 五月激情六月婷婷 | 三级网站免费看 | 久久99综合国产精品亚洲首页 |