/
ZPTagController.m
146 lines (114 loc) · 4.57 KB
/
ZPTagController.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
//
// ZPTagController.m
// ZotPad
//
// Created by Rönkkö Mikko on 9/13/12.
//
//
#import "ZPTagController.h"
#import "ZPItemListViewDataSource.h"
#import <QuartzCore/QuartzCore.h>
@implementation ZPTagController
- (void) configure{
//Get the currently visible items
NSArray* keysForVisibleItems = [[ZPItemListViewDataSource instance] itemKeys];
_tags = [ZPDatabase tagsForItemKeys:keysForVisibleItems];
_estimatedNumberOfRows = ([_tags count]+2)/3;
_nextTagIndex = 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* identifier;
BOOL hasTags;
if(indexPath.section==0 || [_tags count] ==0 ){
identifier = @"CellForNoTags";
hasTags = FALSE;
}
else{
identifier = @"CellWithTags";
hasTags = TRUE;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
if(! hasTags){
cell.textLabel.center = cell.contentView.center;
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = @"No tags";
}
}
if(hasTags){
//Clear existing tags
for(UIView* view in cell.contentView.subviews){
[view removeFromSuperview];
}
//Add tags to cell
NSInteger xForNextTag=5;
while (_nextTagIndex<[_tags count]) {
//TODO: Recycle these labels
UILabel* tagLabel = [[UILabel alloc] init];
tagLabel.text = [_tags objectAtIndex:_nextTagIndex];
[tagLabel sizeToFit];
//If this is too large
if(xForNextTag+tagLabel.frame.size.width > tableView.frame.size.width-5){
if(xForNextTag==5){
tagLabel.frame = CGRectMake(5, 5, tableView.frame.size.width, tagLabel.frame.size.height-5);
}
else break;
}
//Position the tag
tagLabel.frame = CGRectMake(xForNextTag, 5, tagLabel.frame.size.width+10, tagLabel.frame.size.height+10);
//Modify looks
tagLabel.backgroundColor = [UIColor yellowColor];
tagLabel.layer.cornerRadius = tagLabel.frame.size.height/2;
// tagLabel.layer.borderWidth = 2;
tagLabel.textAlignment = UITextAlignmentCenter;
//Add the tag
xForNextTag = xForNextTag + tagLabel.frame.size.width+10;
[cell.contentView addSubview:tagLabel];
DDLogVerbose(@"Adding tag %@ with frame %@",tagLabel.text,NSStringFromCGRect(tagLabel.frame));
_nextTagIndex++;
}
//Update the estimated number of tags
NSInteger difference = indexPath.row+1 + ([_tags count]-_nextTagIndex+2)/3 - _estimatedNumberOfRows;
if(difference!=0){
NSMutableArray* indexPaths = [[NSMutableArray alloc] initWithCapacity:abs(difference)];
NSInteger lowerLimit = MIN(_estimatedNumberOfRows,_estimatedNumberOfRows + difference);
NSInteger upperLimit = MAX(_estimatedNumberOfRows,_estimatedNumberOfRows + difference);
for(NSInteger i = lowerLimit; i < upperLimit; i++){
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
}
/*
_estimatedNumberOfRows = _estimatedNumberOfRows + difference;
if(difference<0){
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
else if(difference>0){
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
DDLogVerbose(@"Adjusting tag rows by %i , count now %i",difference,_estimatedNumberOfRows);
*/
}
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section==0){
return 1;
}
else{
return MAX(1,_estimatedNumberOfRows);
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section ==0){
return @"Selected tags";
}
else{
return @"Available tags";
}
}
@end