-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNSDictionary+TableViewIndexPath.m
163 lines (124 loc) · 4.74 KB
/
NSDictionary+TableViewIndexPath.m
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
//
// NSDictionary+TableViewIndexPath.m
// util
//
// Created by Bálint Róbert on 31/03/16.
// Copyright © 2016 mrnuku. All rights reserved.
//
#import "NSDictionary+TableViewIndexPath.h"
#import <UIKit/UIKit.h>
@implementation NSDictionary (TableViewIndexPath)
/** collects a set of indicies where all sections are unique and rows are undefined
* @return NSMutableArray with the result
*/
- (NSMutableArray<NSIndexPath *> *)_getUniqueSectionIndicies {
__block NSMutableArray<NSIndexPath *> *temp = [NSMutableArray new];
[self enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *key1, id obj1, BOOL *stop1) {
NSUInteger idx = [temp indexOfObjectPassingTest:^BOOL(NSIndexPath *obj2, NSUInteger idx2, BOOL *stop2) {
return (*stop2 = key1.section == obj2.section);
}];
if (idx == NSNotFound) {
[temp addObject:key1];
}
}];
return temp;
}
- (NSInteger)numberOfUniqueSections {
return [self _getUniqueSectionIndicies].count;
}
- (NSInteger)maximumIndexOfSections {
NSArray<NSIndexPath *> *uniqueSectionIndicies = [self _getUniqueSectionIndicies];
NSInteger maximum = 0;
for (NSIndexPath *path in uniqueSectionIndicies) {
maximum = MAX(maximum, path.section);
}
return maximum;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section {
__block NSInteger count = 0;
__block NSInteger maximum = 0;
[self enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *key, id obj, BOOL *stop) {
if (key.section == section) {
maximum = MAX(maximum, key.row);
count++;
}
}];
if (count) {
NSAssert((maximum + 1) == count, @"Found non-continous index set");
}
return count;
}
@end
@implementation NSMutableDictionary (TableViewIndexPath)
- (void)addArrayAsNewSection:(NSArray *)array {
NSInteger numSections = [self numberOfUniqueSections];
for (NSInteger i = 0; i < array.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:numSections];
id obj = [array objectAtIndex:i];
[self setObject:obj forKey:indexPath];
}
}
- (void)removeSection:(NSInteger)section {
__block NSMutableArray<NSIndexPath *> *temp = [NSMutableArray new];
[self enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *key, id obj, BOOL *stop) {
if (key.section == section) {
[temp addObject:key];
}
}];
for (NSIndexPath *path in temp) {
[self removeObjectForKey:path];
}
}
- (void)replaceSection:(NSInteger)section withArray:(NSArray *)array {
[self removeSection:section];
for (NSInteger i = 0; i < array.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:section];
id obj = [array objectAtIndex:i];
[self setObject:obj forKey:indexPath];
}
}
- (BOOL)removeIndexPath:(NSIndexPath *)indexPath {
__block NSMutableArray<NSIndexPath *> *temp = [NSMutableArray new];
__block NSInteger numRowsInSection = 0;
// get upper indicies
[self enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *key, id obj, BOOL *stop) {
BOOL sameSection = key.section == indexPath.section;
if (sameSection) {
numRowsInSection++;
if (key.row > indexPath.row) {
[temp addObject:key];
}
}
}];
// get old objects
NSArray *oldObjects = [self objectsForKeys:temp notFoundMarker:[NSNull null]];
[self removeObjectsForKeys:temp];
[self removeObjectForKey:indexPath];
// move row ids
for (NSUInteger i = 0; i < temp.count; i++) {
NSIndexPath *key = [temp objectAtIndex:i];
id obj = [oldObjects objectAtIndex:i];
NSIndexPath *newKey = [NSIndexPath indexPathForRow:key.row - 1 inSection:key.section];
[self setObject:obj forKey:newKey];
}
// move sections if this was the last object
if (numRowsInSection == 1) {
// get upper sections
[self enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *key, id obj, BOOL *stop) {
if (key.section > indexPath.section) {
[temp addObject:key];
}
}];
oldObjects = [self objectsForKeys:temp notFoundMarker:[NSNull null]];
[self removeObjectsForKeys:temp];
for (NSUInteger i = 0; i < temp.count; i++) {
NSIndexPath *key = [temp objectAtIndex:i];
id obj = [oldObjects objectAtIndex:i];
NSIndexPath *newKey = [NSIndexPath indexPathForRow:key.row inSection:key.section - 1];
[self setObject:obj forKey:newKey];
}
return YES;
}
return NO;
}
@end