Skip to content

Commit a716c11

Browse files
committed
TreeTableView -> TreeView (only-Model concept) interface change.
TreeTableView (as UIView sibling concept) vanished out. On the other hand TreeTable was born as pure DataSource + Delegate concept. TreeTable serves as Proxy between UITableView and Controller that implements Delegate & DataSource protocols.
1 parent 2ed75f7 commit a716c11

File tree

10 files changed

+150
-140
lines changed

10 files changed

+150
-140
lines changed

TreeTable/TreeTable.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// TreeTable.h
3+
//
4+
// Author: kernel@realm
5+
//
6+
7+
#import <UIKit/UIKit.h>
8+
9+
@protocol TreeTableDelegate;
10+
@protocol TreeTableDataSource;
11+
12+
@interface TreeTable : NSObject <UITableViewDelegate, UITableViewDataSource>
13+
@property (weak, nonatomic) IBOutlet id<TreeTableDelegate> delegate;
14+
@property (weak, nonatomic) IBOutlet id<TreeTableDataSource> dataSource;
15+
16+
@property (weak, nonatomic, readonly) UITableView *tableView;
17+
18+
// Expands the row revealing its sibling items.
19+
- (void)expand:(NSIndexPath *)indexPath;
20+
- (BOOL)isExpanded:(NSIndexPath *)indexPath;
21+
// Closes expanded row.
22+
- (void)close:(NSIndexPath *)indexPath;
23+
- (NSArray *)siblings:(NSIndexPath *)indexPath;
24+
- (NSIndexPath *)parent:(NSIndexPath *)indexPath;
25+
26+
- (UITableViewCell *)itemForIndexPath:(NSIndexPath *)indexPath;
27+
- (NSIndexPath *)indexPathForItem:(UITableViewCell *)item;
28+
// Coverts multidimensional indexPath into 2d UITableView-like indexPath.
29+
// This helper method is required to prepare indexPath parameter when calling UITableView methods.
30+
- (NSIndexPath *)tableIndexPathFromTreePath:(NSIndexPath *)indexPath;
31+
@end
32+
33+
34+
@protocol TreeTableDelegate <NSObject>
35+
@optional
36+
- (void)treeView:(UITableView *)treeView clicked:(NSIndexPath *)indexPath;
37+
- (CGFloat)treeView:(UITableView *)treeView heightForItemAtIndexPath:(NSIndexPath *)indexPath;
38+
@end
39+
40+
41+
@protocol TreeTableDataSource <NSObject>
42+
@required
43+
- (BOOL)treeView:(UITableView *)treeView expanded:(NSIndexPath *)indexPath;
44+
- (NSUInteger)treeView:(UITableView *)treeView numberOfSubitems:(NSIndexPath *)indexPath;
45+
- (UITableViewCell *)treeView:(UITableView *)treeView itemForIndexPath:(NSIndexPath *)indexPath;
46+
@end
Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//
22
// TreeView.m
33
//
4+
// Author: kernel@realm
5+
//
46

5-
#import "TreeTableView.h"
7+
#import "TreeTable.h"
68

7-
@interface TreeTableView()
9+
@interface TreeTable()
810
@property (strong, nonatomic, readonly) NSMutableDictionary * model, *directModel;
911
@property (nonatomic) NSUInteger rootItemsCount;
1012

@@ -20,55 +22,26 @@ - (NSUInteger)rowOffsetForIndexPath:(NSIndexPath *)indexPath;
2022
- (NSUInteger)rowOffsetForIndexPath:(NSIndexPath *)indexPath root:(NSIndexPath *)root;
2123
@end
2224

23-
@implementation TreeTableView
24-
25-
- (id)initWithCoder:(NSCoder *)decoder {
26-
self = [super initWithCoder:decoder];
27-
if (self) {
28-
[self setupView];
29-
}
30-
return self;
31-
}
25+
@implementation TreeTable
3226

33-
- (id)initWithFrame:(CGRect)frame {
34-
self = [super initWithFrame:frame];
27+
- (id)init {
28+
self = [super init];
3529
if (self) {
36-
[self setupView];
30+
_model = [NSMutableDictionary dictionary];
31+
_directModel = [NSMutableDictionary dictionary];
3732
}
3833
return self;
3934
}
4035

41-
- (void)setupView {
42-
// Create underlying tableView.
43-
{
44-
_tableView = [[UITableView alloc] initWithFrame:self.bounds];
45-
self.tableView.autoresizingMask = self.autoresizingMask;
46-
47-
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
48-
49-
self.tableView.delegate = self;
50-
self.tableView.dataSource = self;
51-
52-
[self addSubview:self.tableView];
53-
}
54-
55-
_model = [NSMutableDictionary dictionary];
56-
_directModel = [NSMutableDictionary dictionary];
57-
}
58-
59-
- (void)setAutoresizingMask:(UIViewAutoresizing)autoresizingMask {
60-
[super setAutoresizingMask:autoresizingMask];
61-
62-
self.tableView.autoresizingMask = autoresizingMask;
63-
}
36+
#pragma mark Instance methods
6437

6538
- (NSIndexPath *)tableIndexPathFromTreePath:(NSIndexPath *)indexPath {
6639
NSUInteger row = [self rowOffsetForIndexPath:indexPath];
6740
return [NSIndexPath indexPathForRow:row inSection:0];
6841
}
6942

7043
/**
71-
* Converts TreeTableView indexPath to TableView row index.
44+
* Converts TreeTable indexPath to TableView row index.
7245
*/
7346
- (NSUInteger)rowOffsetForIndexPath:(NSIndexPath *)indexPath {
7447
NSUInteger totalCount = 0;
@@ -99,8 +72,8 @@ - (NSUInteger)rowOffsetForIndexPath:(NSIndexPath *)indexPath root:(NSIndexPath *
9972
if (num) {
10073
subitemsCount = num.intValue;
10174
} else {
102-
if ([self.dataSource treeView:self expanded:root]) {
103-
subitemsCount = [self.dataSource treeView:self numberOfSubitems:root];
75+
if ([self.dataSource treeView:self.tableView expanded:root]) {
76+
subitemsCount = [self.dataSource treeView:self.tableView numberOfSubitems:root];
10477
}
10578
}
10679

@@ -234,8 +207,8 @@ - (void)close:(NSIndexPath *)indexPath array:(NSMutableArray *)rows {
234207
- (NSUInteger)numberOfSubitems:(NSIndexPath *)indexPath {
235208
NSUInteger count = 0;
236209

237-
if ([self.dataSource treeView:self expanded:indexPath]) {
238-
NSUInteger subitemsCount = [self.dataSource treeView:self numberOfSubitems:indexPath];
210+
if ([self.dataSource treeView:self.tableView expanded:indexPath]) {
211+
NSUInteger subitemsCount = [self.dataSource treeView:self.tableView numberOfSubitems:indexPath];
239212
for (int i=0; i<subitemsCount; i++ ) {
240213
NSIndexPath * subitemPath = [indexPath indexPathByAddingIndex:i];
241214
count += [self numberOfSubitems:subitemPath];
@@ -313,9 +286,19 @@ - (UITableViewCell*)itemForIndexPath:(NSIndexPath *)indexPath {
313286

314287
#pragma mark UITableViewDelegate, -DataSource
315288

289+
#pragma mark Sections & Footers
290+
291+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
292+
return 1;
293+
}
294+
295+
#pragma mark Cells
296+
316297
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
298+
_tableView = tableView;
299+
317300
// IndexPath==nil: root items.
318-
NSUInteger totalCount = self.rootItemsCount = [self.dataSource treeView:self numberOfSubitems:nil];
301+
NSUInteger totalCount = self.rootItemsCount = [self.dataSource treeView:self.tableView numberOfSubitems:nil];
319302

320303
// Calc subitems of expanded items.
321304
for (int i=0; i<self.rootItemsCount; i++ ) {
@@ -331,14 +314,14 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
331314
NSLog(@"ERR. Invalid itemPath: %@", itemPath);
332315
return nil;
333316
}
334-
return [self.dataSource treeView:self itemForIndexPath:itemPath];
317+
return [self.dataSource treeView:self.tableView itemForIndexPath:itemPath];
335318
}
336319

337320
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
338321
if ([self.delegate respondsToSelector:@selector(treeView:clicked:)]) {
339322
NSIndexPath *ip = [self treeIndexOfRow:indexPath.row];
340323

341-
[self.delegate treeView:self clicked:ip];
324+
[self.delegate treeView:self.tableView clicked:ip];
342325
}
343326
}
344327

@@ -355,7 +338,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
355338
NSLog(@"ERR. Invalid itemPath: %@", itemPath);
356339
return defaultHeight;
357340
}
358-
return [self.delegate treeView:self heightForItemAtIndexPath:itemPath];
341+
return [self.delegate treeView:self.tableView heightForItemAtIndexPath:itemPath];
359342
}
360343

361344
@end

TreeTableView/TreeTableView.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

TreeViewExample.xcodeproj/project.pbxproj

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
184AABA516F111A2008FAF7A /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 184AABA416F111A2008FAF7A /* Default-568h@2x.png */; };
2424
184AABA816F111A2008FAF7A /* Main_iPhone.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 184AABA616F111A2008FAF7A /* Main_iPhone.storyboard */; };
2525
184AABAE16F111A2008FAF7A /* DAViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 184AABAD16F111A2008FAF7A /* DAViewController.m */; };
26-
184AABB716F12AF2008FAF7A /* TreeTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 184AABB616F12AF2008FAF7A /* TreeTableView.m */; };
2726
18A4834316F138D300EA0400 /* Planets.plist in Resources */ = {isa = PBXBuildFile; fileRef = 18A4834216F138D300EA0400 /* Planets.plist */; };
2827
18A4834716F142CD00EA0400 /* DASpanCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 18A4834616F142CD00EA0400 /* DASpanCell.m */; };
2928
18A4834916F1430E00EA0400 /* DASpanCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 18A4834816F1430E00EA0400 /* DASpanCell.xib */; };
3029
18A4837316F2803400EA0400 /* DAPlanetStore.m in Sources */ = {isa = PBXBuildFile; fileRef = 18A4837216F2803400EA0400 /* DAPlanetStore.m */; };
3130
18A4837616F3154100EA0400 /* DAItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 18A4837516F3154000EA0400 /* DAItem.m */; };
31+
18D0B5861764B19F00F8A399 /* TreeTable.m in Sources */ = {isa = PBXBuildFile; fileRef = 18D0B5851764B19F00F8A399 /* TreeTable.m */; };
3232
/* End PBXBuildFile section */
3333

3434
/* Begin PBXFileReference section */
@@ -53,8 +53,6 @@
5353
184AABA716F111A2008FAF7A /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/Main_iPhone.storyboard; sourceTree = "<group>"; };
5454
184AABAC16F111A2008FAF7A /* DAViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DAViewController.h; sourceTree = "<group>"; };
5555
184AABAD16F111A2008FAF7A /* DAViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DAViewController.m; sourceTree = "<group>"; };
56-
184AABB516F12AF2008FAF7A /* TreeTableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TreeTableView.h; sourceTree = "<group>"; };
57-
184AABB616F12AF2008FAF7A /* TreeTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TreeTableView.m; sourceTree = "<group>"; };
5856
18A4834216F138D300EA0400 /* Planets.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Planets.plist; path = TreeViewExample/Planets.plist; sourceTree = "<group>"; };
5957
18A4834516F142CD00EA0400 /* DASpanCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DASpanCell.h; sourceTree = "<group>"; };
6058
18A4834616F142CD00EA0400 /* DASpanCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DASpanCell.m; sourceTree = "<group>"; };
@@ -63,6 +61,8 @@
6361
18A4837216F2803400EA0400 /* DAPlanetStore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DAPlanetStore.m; sourceTree = "<group>"; };
6462
18A4837416F3154000EA0400 /* DAItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DAItem.h; sourceTree = "<group>"; };
6563
18A4837516F3154000EA0400 /* DAItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DAItem.m; sourceTree = "<group>"; };
64+
18D0B5841764B19F00F8A399 /* TreeTable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TreeTable.h; sourceTree = "<group>"; };
65+
18D0B5851764B19F00F8A399 /* TreeTable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TreeTable.m; sourceTree = "<group>"; };
6666
/* End PBXFileReference section */
6767

6868
/* Begin PBXFrameworksBuildPhase section */
@@ -94,7 +94,7 @@
9494
184AAB8216F111A2008FAF7A = {
9595
isa = PBXGroup;
9696
children = (
97-
184AABB416F12AF2008FAF7A /* TreeTableView */,
97+
18D0B5831764B19F00F8A399 /* TreeTable */,
9898
184AAB9416F111A2008FAF7A /* TreeViewExample */,
9999
184AABB816F12AFF008FAF7A /* Res */,
100100
184AAB8D16F111A2008FAF7A /* Frameworks */,
@@ -133,15 +133,6 @@
133133
path = TreeViewExample;
134134
sourceTree = "<group>";
135135
};
136-
184AABB416F12AF2008FAF7A /* TreeTableView */ = {
137-
isa = PBXGroup;
138-
children = (
139-
184AABB516F12AF2008FAF7A /* TreeTableView.h */,
140-
184AABB616F12AF2008FAF7A /* TreeTableView.m */,
141-
);
142-
path = TreeTableView;
143-
sourceTree = "<group>";
144-
};
145136
184AABB816F12AFF008FAF7A /* Res */ = {
146137
isa = PBXGroup;
147138
children = (
@@ -189,6 +180,15 @@
189180
name = Model;
190181
sourceTree = "<group>";
191182
};
183+
18D0B5831764B19F00F8A399 /* TreeTable */ = {
184+
isa = PBXGroup;
185+
children = (
186+
18D0B5841764B19F00F8A399 /* TreeTable.h */,
187+
18D0B5851764B19F00F8A399 /* TreeTable.m */,
188+
);
189+
path = TreeTable;
190+
sourceTree = "<group>";
191+
};
192192
/* End PBXGroup section */
193193

194194
/* Begin PBXNativeTarget section */
@@ -266,10 +266,10 @@
266266
184AAB9B16F111A2008FAF7A /* main.m in Sources */,
267267
184AAB9F16F111A2008FAF7A /* DAAppDelegate.m in Sources */,
268268
184AABAE16F111A2008FAF7A /* DAViewController.m in Sources */,
269-
184AABB716F12AF2008FAF7A /* TreeTableView.m in Sources */,
270269
18A4834716F142CD00EA0400 /* DASpanCell.m in Sources */,
271270
18A4837316F2803400EA0400 /* DAPlanetStore.m in Sources */,
272271
18A4837616F3154100EA0400 /* DAItem.m in Sources */,
272+
18D0B5861764B19F00F8A399 /* TreeTable.m in Sources */,
273273
);
274274
runOnlyForDeploymentPostprocessing = 0;
275275
};

TreeViewExample/DAViewController.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
// Copyright (c) 2013 kernel@realm. All rights reserved.
77
//
88

9-
#import <UIKit/UIKit.h>
109
#import "DASpanCell.h"
1110
#import "DAPlanetStore.h"
1211

1312
@interface DAViewController : UIViewController
14-
@property (strong, nonatomic) IBOutlet TreeTableView *treeView;
13+
@property (strong, nonatomic) IBOutlet TreeTable *treeModel;
14+
15+
16+
@property (strong, nonatomic) IBOutlet UITableView *treeView;
1517
@end

0 commit comments

Comments
 (0)