forked from mrnuku/util
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNSDictionary+TableViewIndexPath.h
More file actions
164 lines (121 loc) · 4.58 KB
/
NSDictionary+TableViewIndexPath.h
File metadata and controls
164 lines (121 loc) · 4.58 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
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
//
// NSDictionary+TableViewIndexPath.h
// util
//
// Created by Bálint Róbert on 31/03/16.
// Copyright © 2016 mrnuku. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSDictionary (TableViewIndexPath)
/** compute the count of all unique sections
* @return NSInteger with the result
*/
- (NSInteger)numberOfUniqueSections;
/** iterates through all indicies and gets the highest index of all sections
* @return NSInteger with the result
*/
- (NSInteger)maximumIndexOfSections;
/** compute the maximum number of rows in the given section
* @param section the section index
* @return NSInteger with the result
*/
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
@end
@interface NSMutableDictionary (TableViewIndexPath)
/** merge an array of objects as a new section
* @param array the source for the new section data
*/
- (void)addArrayAsNewSection:(NSArray *)array;
/** removes any object connected to this section from dictionary
* @param section the index of the section to remove
*/
- (void)removeSection:(NSInteger)section;
/** removes any object then replaces all objects with given array in section
* @param section the index of the section to replace
* @param array the source for the section data
*/
- (void)replaceSection:(NSInteger)section withArray:(NSArray *)array;
/** remove an indexPath from the data and handle the preceding indicies to be updated
* @param indexPath the index path to be removed
* @return BOOL with the resulting objects
*/
- (BOOL)removeIndexPath:(NSIndexPath *)indexPath;
@end
// USAGE EXAMPLE
#if 0
#import "ViewController.h"
#import "SimpleButtonTableViewCell.h"
#import "NSDictionary+NSIndexPath.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController {
NSMutableDictionary *_data;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_data = [NSMutableDictionary new];
NSMutableArray *temp = [NSMutableArray new];
NSInteger j = + arc4random_uniform(16);
for (NSInteger i = 0; i < 32 + j; i++) {
NSString *str = [NSString stringWithFormat:@"Section %li : Row %li", 0L, (long)i];
[temp addObject:str];
}
[_data addArrayAsNewSection:temp];
[temp removeAllObjects];
j = + arc4random_uniform(16);
for (NSInteger i = 0; i < 32 + j; i++) {
NSString *str = [NSString stringWithFormat:@"Section %li : Row %li", 1L, (long)i];
[temp addObject:str];
}
[_data addArrayAsNewSection:temp];
[temp removeAllObjects];
j = + arc4random_uniform(16);
for (NSInteger i = 0; i < 32 + j; i++) {
NSString *str = [NSString stringWithFormat:@"Section %li : Row %li", 2L, (long)i];
[temp addObject:str];
}
[_data addArrayAsNewSection:temp];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_data numberOfUniqueSections];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_data numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SimpleButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"simpleButtonCell"];
NSString *nodeData = [_data objectForKey:indexPath];
cell.stuffLabel.text = nodeData;
[cell setRemoveHandler:^(SimpleButtonTableViewCell *sender) {
NSIndexPath *indexPath2 = [tableView indexPathForCell:sender];
BOOL lastPathInSection = [_data removeIndexPath:indexPath2];
if (!lastPathInSection) {
[tableView deleteRowsAtIndexPaths:@[indexPath2] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else {
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath2.section] withRowAnimation:UITableViewRowAnimationFade];
}
}];
return cell;
}
@end
// SUPLEMENT CODE: SimpleButtonTableViewCell.h, SimpleButtonTableViewCell.m
@interface SimpleButtonTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *stuffLabel;
@property (nonatomic, copy) void (^removeHandler)(SimpleButtonTableViewCell *sender);
@end
@implementation SimpleButtonTableViewCell
- (void)prepareForReuse {
[super prepareForReuse];
self.removeHandler = nil;
}
- (IBAction)removeTouchUpInside:(id)sender {
if (self.removeHandler) {
self.removeHandler(self);
}
self.removeHandler = nil;
}
@end
#endif