1st:tableView: cellForRowAtIndexPath:

引子:

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     static NSString *cellIdentifier = @"SimpleTableItem";       // #1
 4         
 5     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    // #2
 6 
 7     if (cell == nil) {
 8         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 9         cell.selectionStyle = UITableViewCellSelectionStyleNone;
10 
11         UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 65, 65)];
12         imageView.backgroundColor = [UIColor clearColor];
13         imageView.tag = 111;
14         [cell addSubview:imageView];    // #3
15 
16         UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 6, 200, 33)];
17         nameLabel.backgroundColor = [UIColor clearColor];
18         nameLabel.tag = 222;
19         nameLabel.font = [UIFont boldSystemFontOfSize:17];
20         [cell addSubview:nameLabel];
21         
22         UILabel *prepTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 42, 200, 20)];
23         prepTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 42, 200, 20)];
24         prepTimeLabel.backgroundColor = [UIColor clearColor];
25         prepTimeLabel.tag = 333;
26         prepTimeLabel.font = [UIFont fontWithName:@"American Typewriter" size:15];
27         [cell addSubview:prepTimeLabel];    // #4
28     }
29     
30     UIImageView *imageView = (UIImageView *)[cell viewWithTag:111];     // #5
31     UILabel *nameLabel = (UILabel *)[cell viewWithTag:222];
32     UILabel *prepTimeLabel = (UILabel *)[cell viewWithTag:333];
33     
34     imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:[indexPath row]]];     // #6
35     nameLabel.text = [dataArray objectAtIndex:[indexPath row]];
36     prepTimeLabel.text = [NSString stringWithFormat:@"Prep Time:  %@",[timeArray objectAtIndex:[indexPath row]]];
37     
38     return  cell;
39 }

 

#1:cell标识符

  • 如果每个cell结构完全相同,则可以使用一个标识符;
  • 如果每个cell结构不完全相同,则可使用@“Cell%d”, indexPath.row来避免重复标识。

#2:dequeueReusableCellWithIdentifier

  • 用来获取Cell对象,这个对象可能为nil,也可能是之前创建过的,因此之后进行cell == nil的判断;
  • 相当于在Cell池中获取标识符为SimpleTableItem的Cell对象用来复用;
  • 获取到Cell对象可能会有之前添加过的数据(如textLabel.text)或子视图(可使用tag标记后remove),需清除后再复用;
// the cell is being recycled, remove old embedded controls
        UIView *viewToRemove = nil;
        viewToRemove = [cell.contentView viewWithTag:kViewTag];
        if (viewToRemove)
            [viewToRemove removeFromSuperview];
  • 关键点在"一个屏幕显示的cell数量"是有限的。当cell需要显示的时候,从queue里面找,找到了,设置一下内容,显示出来滚动界面当有cell被移出屏幕时,把这个cell丢到queue里面显示新的cell时,如果有相同标识符的cell,就从队列拿一个出来,设置数据,显示出来。至于queue里面会有多少cell,这个会自动控制

#3:[cell addSubview:imageView] 和 [cell.contentView addSubview:imageView]

  • The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.
  • 根据测试,UILabel和UIImageView可以不用cell.contentView,但UIButton需要使用cell.contentView.

#4:尽量实现cell和数据的完全分离

  • 有木有一种万事俱备(cell中的各部分结构都已搭建好),只欠东风(等待填充数据,见#6)的赶脚?

#5:viewWithTag

  • TheviewWithTag: method proceeds through all of the receiver’s descendants (including itself) using a depth-first search, from back to front in the receiver's view hierarchy, looking for a subview with the given tag and returning it if it’s found. (View Programming Guide. Apple)
  •  简而言之,viewWithTag获取subView的基本原则为:深度优先,下层优先。

#6:填充数据

  • 东风来了;
  • cell搭框架(固定不变的)要放在#3#4(即判断cell==nil的if中)的位置,这样才会只添加一次,如果放在#6的位置,会造成多次添加;
  • cell填充数据要放在#6的位置,如果放在#3#4,则使用已有的cell时,原有数据也会存在,就会出现数据的重叠;
  • 所以,可在#3#4处设置tag,在#6处获取并添加数据或进行设置。

 

Ref:

Reflection.xcodeproj (The source of this page.)

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaViewsGuide/WorkingWithAViewHierarchy/WorkingWithAViewHierarchy.html

http://blog.csdn.net/winsdom123456/article/details/7363383

http://blog.csdn.net/dong_007_007/article/details/7580392

http://www.cnblogs.com/smileEvday/archive/2012/03/21/2410529.html

 

posted on 2013-08-28 15:29  CaliosSN  阅读(185)  评论(2)    收藏  举报