1.单元格复用的实现方式


方式1.判断没有可复用的单元格后,由我们自己负责创建 cell 实例。
  • 未完待续。。。
方式2.如果没有取到可复用的单元格,由系统自动依据我们提前说明的类型,帮我们自动创建出 cell的实例。
  • 在 viewDidLoad 中,使用 tableView 的 registerClass 方法,提前注册一个类型,在取不到 cell 的时候,系统会根据我们注册的类型来自动创建 cell。
1
2
//    注册单元格类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
  • 在回答第三问时,使用带有两个参数的 dequeReusableCell 即可
1
2
3
4
5
6
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    配置 cell
   	 cell.textLabel.text = @"你好";
   	 return cell;
}
注意:调用 dequeReusableCell 从队列中取出 cell,如果没有取到,系统会根据我们注册的类型,自动为我们创建。

2.自定义内容视图


1.创建显示内容的视图
2.将这些视图以子视图的形式,添加到 cell.contentView 中即可。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//    **2.自定义内容视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //    配置 cell
    
    UILabel *label = [cell.contentView viewWithTag:100];
    if (label == nil) {//如果 cell 中 没有我们自己定义的这个 label 那么我就创建一个添加到 cell 中
        
        label = [[UILabel alloc]init];
        label.tag = 100;
        label.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);//尽量用 bounds 代替 frame 来设置尺寸
        label.font = [UIFont italicSystemFontOfSize:18];
        
//        添加背景可以覆盖掉之前的内容但是不能这么用。
//        label.backgroundColor = [UIColor lightGrayColor];
        
        label.textColor = [UIColor greenColor];
        label.textAlignment = NSTextAlignmentCenter;
    
        [cell.contentView addSubview:label];
    }
    label.text = [NSString stringWithFormat:@"这是第%ld行",indexPath.row];
  
    return cell;
}

3.自定义辅助视图


s1.创建视图实例。
s2.将视图实例赋值给 cell.accessoryView 即可。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
//    *************** 自定义辅助视图 *************
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //    配置 cell    
    if (indexPath.row == 2) {
        UISwitch *mySwitch = [[UISwitch alloc]init];
        mySwitch.on = YES;
        
        [mySwitch addTarget:self action:@selector(clickSwitch:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView  = mySwitch;
    }else{
//        防止复用时产生多个 Switch 开关
        cell.accessoryView = nil;
    }
	return cell;
}
-(void)clickSwitch:(UISwitch*)sender{
    NSLog(@"当前是%@",sender.on?@"open":@"closs");
}

4.表格结合各种数据模型的显示

前提:表格的行数不定,也叫动态表格(例如新闻列表)

1.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#import "MyTableViewController.h"

@interface MyTableViewController ()
//定义两个数组,分别存储 城市和人口信息
@property(nonatomic,strong) NSArray *allCitys;
@property(nonatomic,strong) NSArray *allPopulation;

@end

@implementation MyTableViewController
//推荐懒加载 延迟加载--在第一次使用 的时候,才创建实例
-(NSArray *)allCitys{
    if (_allCitys == nil) {//!_allCitys or nil == _allCitys 皆可以
        _allCitys = @[@"北京",@"上海",@"广州",@"深圳",@"南京"];
    }
    return _allCitys;
}
-(NSArray *)allPopulation{
/*
    NSNumber *num = @100;//也是一种封装方式
*/
    if (!_allPopulation) {
        _allPopulation = @[@3000,@2000,@2500,@2048,@1500];//??
    }
    return _allPopulation;
}

//
//    if (!_allPopulation) {
//        _allPopulation = @[@"3000",@"2000",@"2500",@"2048",@"1500"];
//    }
//    return _allPopulation;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" /*forIndexPath:indexPath*/];
    
    if (!cell) {      
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
//    第几行显示第几个元素
//    
    cell.textLabel.text = self.allCitys[indexPath.row];
//    
//    cell.detailTextLabel.text = self.allPopulation[indexPath.row];
//
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@万",self.allPopulation[indexPath.row]];
//    
    cell.imageView.image = [UIImage imageNamed:@"head"];
    return cell;
}

#pragma mark - Table view delegate
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    获取点中的 cell 实例
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//???
    NSLog(@"%@的人口是%@",cell.textLabel.text,cell.detailTextLabel.text);   
}
@end