Skip to content

Commit 8981e28

Browse files
committed
项目中的实际使用
1 parent b254225 commit 8981e28

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

KtTableView/KtTableView/KTMainViewController.m

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,32 @@
1212

1313
#import "AFNetworking.h"
1414
#import "KtTableViewBaseItem.h"
15+
#import "KtMainTableModel.h"
1516

1617
@interface KTMainViewController ()
1718

19+
@property (strong, nonatomic) KtMainTableModel *model;
1820

1921
@end
2022

21-
static NSString * const BaseURLString = @"http://1.footballapp.sinaapp.com/mooclist.php";
22-
2323
@implementation KTMainViewController
2424

2525
- (void)viewDidLoad {
2626
[super viewDidLoad];
27-
NSDictionary *parameters = @{@"nextPage": @"0"};
28-
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
29-
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
30-
NSURLSessionDataTask *task = [manager GET:BaseURLString parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
31-
NSDictionary *dic = (NSDictionary *)responseObject;
32-
NSArray *array = dic[@"data"];
33-
for (NSDictionary *dict in array) {
34-
KtTableViewBaseItem *item = [[KtTableViewBaseItem alloc] initWithImage:nil Title:dict[@"title"] SubTitle:nil AccessoryImage:nil];
35-
[self.dataSource appendItem:item];
36-
}
37-
[self.tableView reloadData];
38-
} failure:^(NSURLSessionTask *operation, NSError *error) {
39-
NSLog(@"Error: %@", error);
40-
}];
27+
[self createModel];
28+
[self getFirstPage];
4129
// Do any additional setup after loading the view, typically from a nib.
4230
}
4331

32+
- (void)createModel {
33+
self.model = [[KtMainTableModel alloc] initWithAddress:@"/mooclist.php"];
34+
__weak typeof(self) wSelf = self;
35+
[self.model setCompletionBlock:^(KtBaseModel *model){
36+
__strong typeof(self) sSelf = wSelf;
37+
[sSelf requestBooksSuccess];
38+
}];
39+
}
40+
4441
- (void)createDataSource {
4542
self.dataSource = [[KtMainTableViewDataSource alloc] init]; // 这一步创建了数据源
4643
}
@@ -50,4 +47,18 @@ - (void)didReceiveMemoryWarning {
5047
// Dispose of any resources that can be recreated.
5148
}
5249

50+
- (void)getFirstPage {
51+
self.model.params = @{@"nextPage": @0};
52+
[self.model loadWithShortConnection];
53+
}
54+
55+
- (void)requestBooksSuccess {
56+
for (KtMainTableBookItem *book in self.model.tableViewItem.books) {
57+
KtTableViewBaseItem *item = [[KtTableViewBaseItem alloc] init];
58+
item.itemTitle = book.bookTitle;
59+
[self.dataSource appendItem:item];
60+
}
61+
[self.tableView reloadData];
62+
}
63+
5364
@end

KtTableView/KtTableView/KtTableViewBaseItem.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
@implementation KtTableViewBaseItem
1414

15+
- (instancetype)init {
16+
self = [self initWithImage:nil Title:nil SubTitle:nil AccessoryImage:nil];
17+
return self;
18+
}
19+
1520
- (instancetype)initWithImage:(UIImage *)image Title:(NSString *)title SubTitle:(NSString *)subTitle AccessoryImage:(UIImage *)accessoryImage {
1621
self = [super init];
1722
if (self) {

0 commit comments

Comments
 (0)