Created
July 7, 2011 18:29
-
-
Save danthorpe/1070191 to your computer and use it in GitHub Desktop.
A load more table view controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TableViewController.m | |
// PartialTable | |
// | |
// Created by Abizer Nasir on 07/07/2011. | |
// | |
#import "TableViewController.h" | |
#define kNumberOfItemsToAdd 8 | |
@implementation TableViewController | |
@synthesize items; | |
// Mark: - | |
// Mark: Set up and tear down | |
- (id)init { | |
// New designated initialiser | |
if (!(self = [super initWithStyle:UITableViewStyleGrouped])) { | |
return nil; // Bail! | |
} | |
numberOfItemsToDisplay = kNumberOfItemsToAdd; // Show 10 items at startup | |
return self; | |
} | |
- (id)initWithStyle:(UITableViewStyle)style { | |
// Call out to the new designated initialiser | |
return [self init]; | |
} | |
- (void)dealloc { | |
[items release]; | |
[super dealloc]; | |
} | |
#pragma mark - View lifecycle | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
// Return YES for supported orientations | |
return (interfaceOrientation == UIInterfaceOrientationPortrait); | |
} | |
#pragma mark - Table view data source | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | |
if (numberOfItemsToDisplay == [items count]) { | |
return 1; | |
} | |
return 2; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
if (section == 0) { | |
return numberOfItemsToDisplay; | |
} else { | |
return 1; | |
} | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"ItemCell"; | |
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell, | |
// otherwise, replace it with a button cell. | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (!cell) { | |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; | |
} | |
if (indexPath.section == 0) { | |
cell.textLabel.text = [items objectAtIndex:indexPath.row]; | |
cell.textLabel.textAlignment = UITextAlignmentLeft; | |
cell.textLabel.textColor = [UIColor blackColor]; | |
cell.textLabel.font = [UIFont boldSystemFontOfSize:17.f]; | |
} else { | |
cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Next %d items", @"The text to display to load more content"), kNumberOfItemsToAdd]; | |
cell.textLabel.textAlignment = UITextAlignmentCenter; | |
cell.textLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f]; | |
cell.textLabel.font = [UIFont boldSystemFontOfSize:14.f]; | |
} | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
if (indexPath.section == 1) { | |
NSUInteger i, totalNumberOfItems = [items count]; | |
NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd); | |
NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; | |
for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++) { | |
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; | |
} | |
numberOfItemsToDisplay = newNumberOfItemsToDisplay; | |
[tableView beginUpdates]; | |
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop]; | |
[indexPaths release]; | |
if (numberOfItemsToDisplay == totalNumberOfItems) { | |
[tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop]; | |
} | |
[tableView endUpdates]; | |
// Scroll the cell to the top of the table | |
if (newNumberOfItemsToDisplay < totalNumberOfItems) { | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){ | |
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; | |
}); | |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
} else { | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){ | |
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:totalNumberOfItems-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; | |
}); | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, I applied them to the project at https://github.com/Abizern/PartialTable