-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathFBTableViewController.m
More file actions
103 lines (75 loc) · 3.14 KB
/
Copy pathFBTableViewController.m
File metadata and controls
103 lines (75 loc) · 3.14 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// FBTableViewController.m
// FBLayout
//
// Created by 沈强 on 2017/1/11.
// Copyright © 2017年 qiang.shen. All rights reserved.
//
#import "FBTableViewController.h"
#import "FBFeedModel.h"
#import "UITableView+FBLayout.h"
#import "FBFeedView.h"
#import "FBFPSGraph.h"
@interface FBTableViewController ()
@property(nonatomic, strong)NSMutableArray *feeds;
@property(nonatomic, strong)NSMutableArray<NSMutableArray *> *sections;
@end
@implementation FBTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
FBFPSGraph *graph = [[FBFPSGraph alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 30)
color:[UIColor lightGrayColor]];
[[UIApplication sharedApplication].keyWindow addSubview:graph];
[self loadData];
}
- (void)refresh {
[self loadData];
[self.refreshControl endRefreshing];
}
- (void)loadData {
NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:dataFilePath];
NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *feedDicts = rootDict[@"feed"];
_feeds = @[].mutableCopy;
[feedDicts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[_feeds addObject:[[FBFeedModel alloc] initWithDictionary:obj]];
}];
_sections = [NSMutableArray arrayWithCapacity:1];
[_sections addObject:_feeds];
__weak typeof(self)weakSelf = self;
[self.tableView fb_setCellContnetViewBlockForIndexPath:^UIView *(NSIndexPath *indexPath) {
return [[FBFeedView alloc]initWithModel:weakSelf.sections[indexPath.section][indexPath.row]];
}];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.sections[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self.tableView fb_heightForIndexPath:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self.tableView fb_cellForIndexPath:indexPath];
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2==0) {
[_sections insertObject:_feeds atIndex:indexPath.section];
[tableView insertSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
} else {
[_sections removeObjectAtIndex:indexPath.section];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
}
}
@end