Fork me on GitHub

IGListKit简单入门 CCIGListKitDemo 例子

简单IGListKit入门

简单滴框架示意图

CCIGListKitSimpleFramework.png

代码逻辑

数据项 DemoItem实现 IGListDiffable

1
2
3
4
5
6
7
8
9
10
11
12
13
- (nonnull id<NSObject>)diffIdentifier {
return _name;
}

- (BOOL)isEqualToDiffableObject:(nullable id<IGListDiffable>)object {
if (self == object) {
return YES;
}

DemoItem *item = (DemoItem *)object;

return _controllerClass == item.controllerClass && _controllerIdentifier == item.controllerIdentifier;
}

数据列表

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
- (NSArray *)demos {
if (!_demos) {
_demos = @[
[[DemoItem alloc] initWithName:@"Tail Loading" controllerClass: @"LoadMoreViewController"],
[[DemoItem alloc] initWithName:@"Search Autocomplete" controllerClass: @"SearchViewController"],
[[DemoItem alloc] initWithName:@"Mixed Data" controllerClass: @"MixedDataViewController"],
[[DemoItem alloc] initWithName:@"Nested Adapter" controllerClass: @"NestedAdapterViewController"],
[[DemoItem alloc] initWithName:@"Empty View" controllerClass: @"EmptyViewController"],
[[DemoItem alloc] initWithName:@"Single Section Controller" controllerClass: @"SingleSectionViewController"],
[[DemoItem alloc] initWithName:@"Storyboard" controllerClass: @"SingleSectionViewController" controllerIdentifier: @"demo"],
[[DemoItem alloc] initWithName:@"Single Section Storyboard" controllerClass: @"SingleSectionStoryboardViewController" controllerIdentifier: @"singleSectionDemo"],
[[DemoItem alloc] initWithName:@"Working Range" controllerClass: @"WorkingRangeViewController"],
[[DemoItem alloc] initWithName:@"Diff Algorithm" controllerClass: @"DiffTableViewController"],
[[DemoItem alloc] initWithName:@"Supplementary Views" controllerClass: @"SupplementaryViewController"],
[[DemoItem alloc] initWithName:@"Self-sizing cells" controllerClass: @"SelfSizingCellsViewController"],
[[DemoItem alloc] initWithName:@"Display delegate" controllerClass: @"DisplayViewController"],
[[DemoItem alloc] initWithName:@"Stacked Section Controllers" controllerClass: @"StackedViewController"],
[[DemoItem alloc] initWithName:@"Objc Demo" controllerClass: @"ObjcDemoViewController"],
[[DemoItem alloc] initWithName:@"Objc Generated Model Demo" controllerClass: @"ObjcGeneratedModelDemoViewController"],
[[DemoItem alloc] initWithName:@"Calendar (auto diffing)" controllerClass: @"CalendarViewController"],
[[DemoItem alloc] initWithName:@"Dependency Injection" controllerClass: @"AnnouncingDepsViewController"],
[[DemoItem alloc] initWithName:@"Reorder Cells" controllerClass: @"ReorderableViewController"],
[[DemoItem alloc] initWithName:@"Reorder Stacked Section Controllers" controllerClass: @"ReorderableStackedViewController"],
];
}

return _demos;
}

绑定数据列表

1
2
3
- (NSArray<id <IGListDiffable>> *)objectsForListAdapter:(IGListAdapter *)listAdapter {
return self.demos;
}

绑定IGListSectionController

1
2
3
4
5
6
7
- (IGListSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object {
return [DemoSectionController new];
}

- (nullable UIView *)emptyViewForListAdapter:(IGListAdapter *)listAdapter {
return nil;
}

adapter 与 collectionView 关联

1
2
self.adapter.collectionView = self.collectionView;
self.adapter.dataSource = self;

DemoSectionController 关联 cell

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
- (CGSize)sizeForItemAtIndex:(NSInteger)index {
return CGSizeMake(self.collectionContext.containerSize.width, 55);
}

- (UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index {
LabelCell *cell = [self.collectionContext dequeueReusableCellOfClass:[LabelCell class] forSectionController:self atIndex:index];
cell.text = self.item.name;

return cell;
}

- (void)didUpdateToObject:(id)object {
self.item = object;
}

- (void)didSelectItemAtIndex:(NSInteger)index {
NSString *classString = self.item.controllerClass;
NSString *controllerIdentifier = self.item.controllerIdentifier;
CCDebugPrint(classString);

UIViewController *vc = nil;
if (controllerIdentifier) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Demo" bundle: nil];
vc = [storyboard instantiateViewControllerWithIdentifier:controllerIdentifier];
} else {
vc = [NSClassFromString(classString) new];
}

if (vc) {
vc.title = self.item.name;
[self.viewController.navigationController pushViewController:vc animated: NO];
} else {
CCDebugWarningPrint([NSString stringWithFormat:@"%@ : [The interface may not be implemented] ", classString]);
}
}

更多细节

查查看CCIGListKitDemo