Skip to content

Commit 6475496

Browse files
committed
实际使用
1 parent e8f2e45 commit 6475496

4 files changed

Lines changed: 58 additions & 18 deletions

File tree

KtTableView/KtTableView/KTMainViewController.m

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
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.

KtTableView/KtTableView/KtMainTableViewCell.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//
88

99
#import <UIKit/UIKit.h>
10+
#import "KtBaseTableViewCell.h"
1011

11-
@interface KtMainTableViewCell : UITableViewCell
12+
@interface KtMainTableViewCell : KtBaseTableViewCell
1213

1314
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// KtMainTableViewDataSource.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtTableViewDataSource.h"
10+
11+
@interface KtMainTableViewDataSource : KtTableViewDataSource
12+
13+
@end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// KtMainTableViewDataSource.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtMainTableViewDataSource.h"
10+
#import "KtMainTableViewCell.h"
11+
12+
#import "KtTableViewSectionObject.h" // 这个实际使用时应该是对应的子类
13+
#import "KtTableViewBaseItem.h" // 这个实际使用时应该是对应的子类
14+
15+
@implementation KtMainTableViewDataSource
16+
17+
- (instancetype)init
18+
{
19+
self = [super init];
20+
if (self) {
21+
KtTableViewSectionObject *firstSectionObject = [[KtTableViewSectionObject alloc] initWithItemArray:[NSMutableArray arrayWithObjects:
22+
[[KtTableViewBaseItem alloc] initWithImage:nil Title:@"第一条消息" SubTitle:nil AccessoryImage:nil],
23+
[[KtTableViewBaseItem alloc] initWithImage:nil Title:@"第二条消息" SubTitle:nil AccessoryImage:nil],
24+
[[KtTableViewBaseItem alloc] initWithImage:nil Title:@"第三条消息" SubTitle:nil AccessoryImage:nil],
25+
[[KtTableViewBaseItem alloc] initWithImage:nil Title:@"第四条消息" SubTitle:nil AccessoryImage:nil],
26+
[[KtTableViewBaseItem alloc] initWithImage:nil Title:@"第五条消息" SubTitle:nil AccessoryImage:nil],
27+
nil]];
28+
self.sections = [NSMutableArray arrayWithObject: firstSectionObject];
29+
}
30+
return self;
31+
}
32+
33+
- (Class)tableView:(UITableView *)tableView cellClassForObject:(KtTableViewBaseItem *)object {
34+
return [KtMainTableViewCell class];
35+
}
36+
37+
@end

0 commit comments

Comments
 (0)