Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 09ebc2f7a4
Fetching contributors…

Cannot retrieve contributors at this time

file 227 lines (168 sloc) 6.753 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
//
// JAObjectListView.m
// JAListView
//
// Created by Josh Abernathy on 12/6/10.
// Copyright 2010 Maybe Apps. All rights reserved.
//

#import "JAObjectListView.h"
#import "JAObjectListViewItem.h"

@interface JAListView (Private)
- (NSArray *)cachedViews;
@end

@interface JAObjectListView ()
- (void)reallyAddListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index;

@property (nonatomic, retain) NSMutableArray *sectionRowViews;
@end


@implementation JAObjectListView

- (void)dealloc {
    self.sectionRowViews = nil;
    
    [super dealloc];
}


#pragma mark JAListView

- (void)setup {
    [super setup];
    
    self.sectionDataSource = self;
    self.sectionRowViews = [NSMutableArray array];
}


#pragma mark JASectionedListView

- (void)setSectionDataSource:(id<JASectionedListViewDataSource>)newDataSource {
    NSAssert([newDataSource isKindOfClass:[self class]], @"Do not set the sectionedDataSource manually.");
    
    [super setSectionDataSource:newDataSource];
}


#pragma mark JASectionedListViewDataSource

- (NSUInteger)numberOfSectionsInListView:(JASectionedListView *)listView {
    return [self numberOfSections];
}

- (NSUInteger)listView:(JASectionedListView *)listView numberOfViewsInSection:(NSUInteger)section {
    return [self viewsInSection:section].count;
}

- (JAListViewItem *)listView:(JAListView *)listView sectionHeaderViewForSection:(NSUInteger)section {
    return [[self.sectionRowViews objectAtIndex:section] objectAtIndex:0];
}

- (JAListViewItem *)listView:(JAListView *)listView viewForSection:(NSUInteger)section index:(NSUInteger)index {
    return [[self viewsInSection:section] objectAtIndex:index];
}


#pragma mark API

@synthesize sectionRowViews;

- (void)addListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index {
    [self reallyAddListViewItem:view inSection:section atIndex:index + 1];
}

- (void)reallyAddListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index {
    NSMutableArray *sectionViews = nil;
    if(section < self.sectionRowViews.count) {
        sectionViews = [self.sectionRowViews objectAtIndex:section];
    } else if(section != NSNotFound) {
        sectionViews = [NSMutableArray array];
        [self.sectionRowViews insertObject:sectionViews atIndex:section];
    } else {
        NSAssert1(NO, @"Tried to insert view into a non-existent section: %@", view);
    }
    
    if(index != NSNotFound) {
        [sectionViews insertObject:view atIndex:index];
    } else {
        NSAssert1(NO, @"Tried to insert view into a non-existent row: %@", view);
    }
}

- (void)addListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section {
    [self addListViewItem:view inSection:section atIndex:[self viewsInSection:section].count];
}

- (void)removeListViewItemInSection:(NSUInteger)section atIndex:(NSUInteger)index {
    NSMutableArray *sectionViews = [self.sectionRowViews objectAtIndex:section];
    
    NSUInteger trueIndex = index + 1;
    JAObjectListViewItem *view = [sectionViews objectAtIndex:trueIndex];
[self unmarkViewBeingUsedForInertialScrolling:view];
    
    [self deselectView:view];
    
    [sectionViews removeObjectAtIndex:trueIndex];
}

- (void)addListViewItem:(JAObjectListViewItem *)view forHeaderForSection:(NSUInteger)section {
    NSMutableArray *sectionViews = [NSMutableArray array];
    [self.sectionRowViews insertObject:sectionViews atIndex:section];
    [sectionViews insertObject:view atIndex:0];
}

- (void)removeListViewItemForHeaderForSection:(NSUInteger)section {
    NSArray *views = [self.sectionRowViews objectAtIndex:section];
    for(JAObjectListViewItem *item in views) {
        [self unmarkViewBeingUsedForInertialScrolling:item];
        
        [self deselectView:item];
    }
    
    [self.sectionRowViews removeObjectAtIndex:section];
}

- (void)removeListViewItem:(JAListViewItem *)view {
    [self unmarkViewBeingUsedForInertialScrolling:view];
    
    [self deselectView:view];
    
    for(NSUInteger sectionIndex = 0; sectionIndex < self.sectionRowViews.count; sectionIndex++) {
        NSMutableArray *sectionViews = [self.sectionRowViews objectAtIndex:sectionIndex];
        NSUInteger index = [sectionViews indexOfObject:view];
        if(index != NSNotFound) {
            if(index == 0) {
                [self removeListViewItemForHeaderForSection:sectionIndex];
            } else {
                [sectionViews removeObjectAtIndex:index];
            }
        }
    }
}

- (void)removeAllListViewItems {
    [self clearViewsBeingUsedForInertialScrolling];
    [self deselectAllViews];
    [self.sectionRowViews removeAllObjects];
}

- (NSArray *)viewsInSection:(NSUInteger)section {
    NSParameterAssert(section < self.sectionRowViews.count);
    
    NSMutableArray *views = [[[self.sectionRowViews objectAtIndex:section] mutableCopy] autorelease];
    if(views.count < 1) return nil;
    
    [views removeObjectAtIndex:0];
    return views;
}

- (NSUInteger)numberOfSections {
    return self.sectionRowViews.count;
}

- (JAObjectListViewItem *)viewItemForObject:(id)object {
    for(NSArray *views in self.sectionRowViews) {
        for(JAObjectListViewItem *item in views) {
            if([item.object isEqual:object]) {
                return item;
            }
        }
    }
    
    return nil;
}

- (NSIndexPath *)indexPathForObject:(id)object {
NSAssert(object != nil, @"Invalid to look for the index path of a nil object.");

    NSUInteger section = 0;
    for(NSArray *views in self.sectionRowViews) {
        
        NSUInteger index = 0;
        for(JAObjectListViewItem *item in views) {
            if([item.object isEqual:object]) {
                if(index == 0) {
                    return [NSIndexPath indexPathForIndex:JASectionedListViewHeaderIndex inSection:section];
                } else {
                    return [NSIndexPath indexPathForIndex:index - 1 inSection:section];
                }
            }
            
            index++;
        }
        
        section++;
    }
    
    return nil;
}

- (JAListViewItem *)headerForSection:(NSUInteger)section {
return [[self.sectionRowViews objectAtIndex:section] objectAtIndex:0];
}

- (void)removeListViewItemForObject:(id)object {
    [self removeListViewItem:[self viewItemForObject:object]];
}

- (NSArray *)allObjects {
NSMutableArray *objects = [NSMutableArray array];
for(NSArray *views in self.sectionRowViews) {
for(JAObjectListViewItem *item in views) {
if(item.object != nil) [objects addObject:item.object];
}
}

return objects;
}

- (NSString *)debugString {
return [NSString stringWithFormat:@"%@", self.sectionRowViews];
}

@end
Something went wrong with that request. Please try again.