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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
// IBTTableViewInfo.m
// IBTTableViewKit
//
// Created by Xummer on 15/1/5.
// Copyright (c) 2015年 Xummer. All rights reserved.
//
#define IBT_GROUPED_TABLEVIEW_TOP_MARGIN (10.0f)
#import "IBTTableViewInfo.h"
/*
@{ "showIndex" : NO }
*/
NSString * const TInfoShowRightIndexKey = @"showIndex";
#import "IBTTableView.h"
#import "IBTTableViewCell.h"
#import "IBTTableViewInfoDelegate.h"
#import "UILabel+SizeCalculate.h"
static NSString *IBTTableViewCellIdentifier = @"IBTTableViewCell";
@interface IBTTableViewInfo ()
<
UITableViewDelegate,
UITableViewDataSource
>
{
IBTTableView *_tableView;
NSMutableArray *_arrSections;
}
@end
@implementation IBTTableViewInfo
#pragma mark - Life Cycle
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
self = [super init];
if (!self) {
return nil;
}
_tableView = [[IBTTableView alloc] initWithFrame:frame style:style];
[_tableView registerClass:[IBTTableViewCell class]
forCellReuseIdentifier:IBTTableViewCellIdentifier];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
return self;
}
- (void)dealloc {
_tableView.dataSource = nil;
_tableView.delegate = nil;
_tableView = nil;
}
#pragma mark - Public Method
- (IBTTableView *)getTableView {
return _tableView;
}
- (IBTTableViewSectionInfo *)getSectionAt:(NSUInteger)secIndex {
if (secIndex < [_arrSections count]) {
return _arrSections[ secIndex ];
}
return nil;
}
- (void)addSection:(IBTTableViewSectionInfo *)section {
if (![section isKindOfClass:[IBTTableViewSectionInfo class]]) {
return;
}
if (!_arrSections) {
_arrSections = [NSMutableArray array];
}
[_arrSections addObject:section];
}
- (void)removeSectionAt:(NSUInteger)secIndex {
if (secIndex < [_arrSections count]) {
[_arrSections removeObjectAtIndex:secIndex];
// [_tableView reloadDataAnimated];
}
}
- (NSUInteger)getSectionCount {
return [_arrSections count];
}
- (void)clearAllSection {
_arrSections = nil;
}
- (IBTTableViewCellInfo *)getCellAtSection:(NSUInteger)section row:(NSUInteger)row {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
return [secInfo getCellAt:row];
}
- (void)removeCellAt:(NSIndexPath *)indexPath {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:indexPath.section];
[secInfo removeCellAt:indexPath.row];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
return [secInfo getCellCount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:IBTTableViewCellIdentifier
forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
IBTTableViewCellInfo *cellInfo =
[self getCellAtSection:indexPath.section row:indexPath.row];
cellInfo.cell = (IBTTableViewCell *)cell;
if (cellInfo.makeTarget &&
[cellInfo.makeTarget respondsToSelector:cellInfo.makeSel])
{
IMP imp = [cellInfo.makeTarget methodForSelector:cellInfo.makeSel];
void (*func)(id, SEL, id) = (void *)imp;
func(cellInfo.makeTarget, cellInfo.makeSel, cellInfo);
}
}
// Section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self getSectionCount];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
switch (tableView.style) {
case UITableViewStyleGrouped:
{
return nil;
}
break;
default:
{
return [secInfo getUserInfoValueForKey:SInfoHeaderTitleKey];
}
break;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
return [secInfo getUserInfoValueForKey:SInfoFooterTitleKey];
}
// Editing
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
IBTTableViewCellInfo *cellInfo = [self getCellAtSection:indexPath.section row:indexPath.row];
return [[cellInfo getUserInfoValueForKey:CInfoSwipeAbleKey] boolValue];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
IBTTableViewCellInfo *cellInfo =
[self getCellAtSection:indexPath.section row:indexPath.row];
return cellInfo.fCellHeight;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
IBTTableViewCellInfo *cellInfo =
[self getCellAtSection:indexPath.section row:indexPath.row];
if (![cellInfo getUserInfoValueForKey:CInfoEditorKey] &&
cellInfo.actionTarget &&
[cellInfo.actionTarget respondsToSelector:cellInfo.actionSel])
{
IMP imp = [cellInfo.actionTarget methodForSelector:cellInfo.actionSel];
void (*func)(id, SEL, id) = (void *)imp;
func(cellInfo.actionTarget, cellInfo.actionSel, cellInfo);
}
}
// Section header & footer information. Views are preferred over title should you decide to provide both
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
CGFloat fH = secInfo.fHeaderHeight;
switch (tableView.style) {
case UITableViewStyleGrouped:
{
if (fH < 0) {
NSString *nsHTitle = [secInfo getUserInfoValueForKey:SInfoHeaderTitleKey];
fH = IBT_SECTION_HEADER_BOTTOM_MARGIN + IBT_SECTION_HEADER_TOP_MARGIN +
[UILabel getHeightWithText:nsHTitle
font:[UIFont systemFontOfSize:IBT_SECTION_HEADER_DEFAULT_FONT_SIZE]
andWidth:CGRectGetWidth(tableView.frame) - 2 * IBT_CELL_MARGIN];
}
}
break;
default:
break;
}
return fH;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
CGFloat fH = secInfo.fFooterHeight;
switch (tableView.style) {
case UITableViewStyleGrouped:
{
if (fH < 0) {
NSString *nsFTitle = [secInfo getUserInfoValueForKey:SInfoFooterTitleKey];
fH = IBT_SECTION_FOOTER_BOTTOM_MARGIN + IBT_SECTION_FOOTER_TOP_MARGIN +
[UILabel getHeightWithText:nsFTitle
font:[UIFont systemFontOfSize:IBT_SECTION_FOOTER_DEFAULT_FONT_SIZE]
andWidth:CGRectGetWidth(tableView.frame) - 2 * IBT_CELL_MARGIN];
}
}
break;
default:
break;
}
return fH;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
UIView *view = nil;
switch (tableView.style) {
case UITableViewStyleGrouped:
{
if (!view) {
NSString *nsHTitle = [secInfo getUserInfoValueForKey:SInfoHeaderTitleKey];
if (nsHTitle.length > 0) {
CGRect frame = (CGRect){
.origin = CGPointZero,
.size.width = CGRectGetWidth(tableView.frame),
.size.height = IBT_GROUP_SECTION_HEADER_HEIGHT
};
view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor clearColor];
frame = (CGRect){
.origin.x = IBT_CELL_MARGIN,
.origin.y = IBT_SECTION_HEADER_TOP_MARGIN,
.size.width = CGRectGetWidth(view.frame) - 2 * IBT_CELL_MARGIN,
.size.height = CGRectGetHeight(view.frame) - IBT_SECTION_HEADER_BOTTOM_MARGIN
};
UILabel *tLabel = [[UILabel alloc] initWithFrame:frame];
tLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tLabel.numberOfLines = 0;
tLabel.textAlignment = NSTextAlignmentLeft;
tLabel.backgroundColor = [UIColor clearColor];
tLabel.font = [UIFont systemFontOfSize:IBT_SECTION_HEADER_DEFAULT_FONT_SIZE];
tLabel.textColor = IBT_SECTION_HEADER_DEFAULT_COLOR;
tLabel.text = nsHTitle;
[view addSubview:tLabel];
CGSize labelSize = [tLabel sizeThatFits:tLabel.frame.size];
if (labelSize.width > CGRectGetWidth(tLabel.frame)) {
labelSize.width = CGRectGetWidth(tLabel.frame);
}
frame = view.frame;
frame.size.height = labelSize.height;
view.frame = frame;
}
}
}
break;
default:
{
view = [secInfo getUserInfoValueForKey:SInfoHeaderKey];
}
break;
}
return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
IBTTableViewSectionInfo *secInfo = [self getSectionAt:section];
UIView *view = nil;
switch (tableView.style) {
case UITableViewStyleGrouped:
{
if (!view) {
NSString *nsFTitle = [secInfo getUserInfoValueForKey:SInfoFooterTitleKey];
if (nsFTitle.length > 0) {
if (0 == section && !_tableView.tableHeaderView) {
_tableView.tableHeaderView = [[UIView alloc] initWithFrame:(CGRect){
.origin = CGPointZero,
.size.width = 0,
.size.height = IBT_GROUPED_TABLEVIEW_TOP_MARGIN
}];
}
CGRect frame = (CGRect){
.origin = CGPointZero,
.size.width = CGRectGetWidth(tableView.frame),
.size.height = 40
};
view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor clearColor];
frame = (CGRect){
.origin.x = IBT_CELL_MARGIN,
.origin.y = IBT_SECTION_FOOTER_TOP_MARGIN,
.size.width = CGRectGetWidth(view.frame) - 2 * IBT_CELL_MARGIN,
.size.height = CGRectGetHeight(view.frame) - IBT_SECTION_FOOTER_BOTTOM_MARGIN
};
UILabel *tLabel = [[UILabel alloc] initWithFrame:frame];
tLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tLabel.numberOfLines = 0;
tLabel.textAlignment = NSTextAlignmentCenter;
tLabel.backgroundColor = [UIColor clearColor];
tLabel.font = [UIFont systemFontOfSize:IBT_SECTION_FOOTER_DEFAULT_FONT_SIZE];
tLabel.textColor = IBT_SECTION_FOOTER_DEFAULT_COLOR;
tLabel.text = nsFTitle;
[view addSubview:tLabel];
CGSize labelSize = [tLabel sizeThatFits:tLabel.frame.size];
if (labelSize.width > CGRectGetWidth(tLabel.frame)) {
labelSize.width = CGRectGetWidth(tLabel.frame);
}
frame = view.frame;
frame.size.height = labelSize.height;
view.frame = frame;
}
}
}
break;
default:
{
view = [secInfo getUserInfoValueForKey:SInfoFooterKey];
}
break;
}
return view;
}
// Edit
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([_delegate respondsToSelector:@selector(commitEditingForRowAtIndexPath:Cell:)]) {
[_delegate commitEditingForRowAtIndexPath:indexPath
Cell:[self getCellAtSection:indexPath.section row:indexPath.row]];
}
}
//
- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
if ([_delegate respondsToSelector:@selector(accessoryButtonTappedForRowWithIndexPath:Cell:)])
{
[_delegate accessoryButtonTappedForRowWithIndexPath:indexPath
Cell:[self getCellAtSection:indexPath.section row:indexPath.row]];
}
}
@end