-
Notifications
You must be signed in to change notification settings - Fork 24
/
JASectionedListView.m
141 lines (104 loc) · 4.11 KB
/
JASectionedListView.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
//
// JASectionedListView.m
// DemoApp
//
// Created by Josh Abernathy on 11/26/10.
// Copyright 2010 Maybe Apps, LLC. All rights reserved.
//
#import "JASectionedListView.h"
#import "JAListViewItem.h"
@interface JASectionedListView ()
- (NSUInteger)numberOfTotalViews;
@end
@implementation JASectionedListView
#pragma mark JAListView
- (void)setup {
[super setup];
self.dataSource = self;
}
- (void)setDataSource:(id<JAListViewDataSource>)newDataSource {
NSAssert1([newDataSource isKindOfClass:[self class]], @"Please use sectionDataSource for %@.", NSStringFromClass([self class]));
[super setDataSource:newDataSource];
}
#pragma mark JAListViewDataSource
- (NSUInteger)numberOfItemsInListView:(JAListView *)listView {
return [self numberOfTotalViews];
}
- (JAListViewItem *)listView:(JAListView *)listView viewAtIndex:(NSUInteger)index {
NSUInteger relativeIndex;
NSUInteger section;
[self getSection:§ion andIndex:&relativeIndex fromAbsoluteIndex:index];
if(relativeIndex == JASectionedListViewHeaderIndex) {
return [self.sectionDataSource listView:self sectionHeaderViewForSection:section];
}
return [self.sectionDataSource listView:self viewForSection:section index:relativeIndex];
}
#pragma mark API
@synthesize sectionDataSource;
- (void)getSection:(NSUInteger *)section andIndex:(NSUInteger *)index fromAbsoluteIndex:(NSUInteger)absoluteIndex {
*index = absoluteIndex;
for(NSUInteger sectionIndex = 0; sectionIndex < [self numberOfSections]; sectionIndex++) {
NSUInteger numberOfViews = [self numberOfViewsInSection:sectionIndex] + 1;
if(*index < numberOfViews) {
*section = sectionIndex;
break;
} else {
*index -= numberOfViews;
}
}
// We want index 0 to be the first row, not the header view, so we reinterpret that as JASectionedListViewHeaderIndex.
if(*index == 0) {
*index = JASectionedListViewHeaderIndex;
} else {
*index -= 1;
}
}
- (NSIndexPath *)indexPathFromAbsoluteIndex:(NSUInteger)absoluteIndex {
NSUInteger section;
NSUInteger index;
[self getSection:§ion andIndex:&index fromAbsoluteIndex:absoluteIndex];
return [NSIndexPath indexPathForIndex:index inSection:section];
}
- (NSUInteger)absoluteIndexFromSection:(NSUInteger)section index:(NSUInteger)index {
NSUInteger absoluteIndex = 0;
for(NSUInteger sectionIndex = 0; sectionIndex < section; sectionIndex++) {
NSUInteger numberOfViews = [self numberOfViewsInSection:sectionIndex] + 1;
absoluteIndex += numberOfViews;
}
absoluteIndex += index + 1;
return absoluteIndex;
}
- (NSUInteger)absoluteIndexFromIndexPath:(NSIndexPath *)indexPath {
return [self absoluteIndexFromSection:indexPath.section index:indexPath.index];
}
- (NSIndexPath *)indexPathForView:(JAListViewItem *)view {
return [self indexPathFromAbsoluteIndex:(NSUInteger) [self indexForView:view]];
}
- (BOOL)isViewSectionHeaderView:(JAListViewItem *)view {
return [self indexPathForView:view].index == JASectionedListViewHeaderIndex;
}
- (JAListViewItem *)viewInSection:(NSUInteger)section atIndex:(NSUInteger)index {
return [self viewAtIndex:[self absoluteIndexFromSection:section index:index]];
}
- (JAListViewItem *)viewAtIndexPath:(NSIndexPath *)indexPath {
return [self viewInSection:indexPath.section atIndex:indexPath.index];
}
- (NSUInteger)numberOfSections {
return [self.sectionDataSource numberOfSectionsInListView:self];
}
- (NSUInteger)numberOfViewsInSection:(NSUInteger)section {
return [self.sectionDataSource listView:self numberOfViewsInSection:section];
}
- (NSUInteger)numberOfTotalViews {
// one section header view per section
NSUInteger totalCount = [self numberOfSections];
for(NSUInteger sectionIndex = 0; sectionIndex < [self numberOfSections]; sectionIndex++) {
totalCount += [self numberOfViewsInSection:sectionIndex];
}
return totalCount;
}
- (void)setSectionDataSource:(id <JASectionedListViewDataSource>)newDataSource {
sectionDataSource = newDataSource;
[self reloadData];
}
@end