88
99#import " KTMainViewController.h"
1010#import " KtMainTableViewCell.h"
11+ #import " KtMainTableViewDataSource.h"
1112
12- @interface KTMainViewController () <UITableViewDelegate, UITableViewDataSource >
13+ @interface KTMainViewController () <UITableViewDelegate>
1314
1415@property (strong , nonatomic ) NSMutableArray *items;
16+ @property (strong , nonatomic ) KtMainTableViewDataSource *dataSource;
1517
1618@end
1719
@@ -26,27 +28,14 @@ - (void)viewDidLoad {
2628 [self .view addSubview: self .tableView];
2729
2830 self.tableView .delegate = self;
29- self.tableView .dataSource = self;
31+
32+ self.dataSource = [[KtMainTableViewDataSource alloc ] init ]; // 这一步创建了数据源
33+ self.tableView .dataSource = self.dataSource ; // 绑定了数据源
3034 self.tableView .tableFooterView = [[UIView alloc ] init ]; // 去掉多余分割线
3135
3236 // Do any additional setup after loading the view, typically from a nib.
3337}
3438
35- #pragma mark - UITableViewDataSource
36- - (UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath {
37- static NSString *identifier = @" KtMainTableViewCell" ;
38- [self .tableView registerClass: [KtMainTableViewCell class ] forCellReuseIdentifier: identifier];
39-
40- KtMainTableViewCell *cell = [self .tableView dequeueReusableCellWithIdentifier: identifier forIndexPath: indexPath]; // 会调用到 initWithStyle:reuseIdentifier: 方法
41- cell.textLabel .text = [self .items objectAtIndex: [indexPath row ]];
42-
43- return cell;
44- }
45-
46- - (NSInteger )tableView : (UITableView *)tableView numberOfRowsInSection : (NSInteger )section {
47- return self.items .count ;
48- }
49-
5039- (void )didReceiveMemoryWarning {
5140 [super didReceiveMemoryWarning ];
5241 // Dispose of any resources that can be recreated.
0 commit comments