Skip to content
Browse files

Implemented a "rotate" function on the grouped data source that shift…

…s the

last item to the top and moves each item down one, which results in objects
changing sections.

The LRTableModelEvent types have been updated to reflect that they relate to
rows, and the factory methods have been updated to allow the section to be
specified when sending row update events.
  • Loading branch information...
1 parent 4e553f4 commit 10523be290d6ef91220309e4f1dda1d6ed42522a @lukeredpath committed Aug 11, 2010
View
2 Classes/ExamplesIndexViewController.m
@@ -28,7 +28,7 @@ - (void)dealloc
- (void)loadExamplesFromPlistNamed:(NSString *)plistName inBundle:(NSBundle *)bundle
{
examples = [[NSArray arrayWithContentsOfFile:[bundle pathForResource:plistName ofType:@"plist"]] retain];
- [self notifyListeners:[LRTableModelEvent refreshed]];
+ [self notifyListeners:[LRTableModelEvent refreshedData]];
}
- (NSInteger)numberOfSections;
View
1 Classes/GroupedTableModel/GroupedTableModel.h
@@ -15,4 +15,5 @@
NSMutableArray *sectionTitles;
}
- (void)setSections:(NSArray *)arrayOfSectionArrays sectionTitles:(NSArray *)titles;
+- (void)rotateLastItem;
@end
View
30 Classes/GroupedTableModel/GroupedTableModel.m
@@ -41,14 +41,42 @@ - (void)setSections:(NSArray *)arrayOfSectionArrays sectionTitles:(NSArray *)tit
{
[sections setArray:arrayOfSectionArrays];
[sectionTitles setArray:titles];
- [self notifyListeners:[LRTableModelEvent refreshed]];
+ [self notifyListeners:[LRTableModelEvent refreshedData]];
}
- (NSString *)headerforSection:(NSInteger)section;
{
return [sectionTitles objectAtIndex:section];
}
+/*
+ * this takes the last object from the list, pushes it to the top
+ * and moves everything down one (which means the last object in
+ * each section moves to the top of the next section
+ */
+- (void)rotateLastItem;
+{
+ NSMutableArray *lastSection = [sections lastObject];
+
+ id lastObject = [[lastSection lastObject] retain];
+ NSInteger indexOfLastObject = [lastSection indexOfObject:lastObject];
+ [lastSection removeLastObject];
+ [self notifyListeners:[LRTableModelEvent deletedRow:indexOfLastObject section:[sections indexOfObject:lastSection]]];
+
+ for (NSMutableArray *section in sections) {
+ [section insertObject:lastObject atIndex:0];
+ [self notifyListeners:[LRTableModelEvent insertionAtRow:0 section:[sections indexOfObject:section]]];
+ [lastObject release];
+
+ if (section != [sections lastObject]) {
+ lastObject = [[section lastObject] retain];
+ NSInteger indexOfLastObject = [section indexOfObject:lastObject];
+ [section removeLastObject];
+ [self notifyListeners:[LRTableModelEvent insertionAtRow:indexOfLastObject section:[sections indexOfObject:section]]];
+ }
+ }
+}
+
#pragma mark -
#pragma mark LRTableModel
View
7 Classes/GroupedTableModel/GroupedTableViewController.m
@@ -28,6 +28,8 @@ - (void)viewDidLoad
self.title = @"Grouped";
self.tableView.dataSource = self.tableModel;
+ self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(rotateButtonTapped:)] autorelease];
+
// divide up the repositories from the sample data into groups of x using some basic logic
NSInteger numberOfItemsInGroup = 5;
@@ -62,4 +64,9 @@ - (void)configureCell:(UITableViewCell *)cell forObject:(id)object atIndexPath:(
cell.textLabel.text = object;
}
+- (void)rotateButtonTapped:(id)sender
+{
+ [self.tableModel rotateLastItem];
+}
+
@end
View
16 Classes/LRTableModel/LRTableModelEvent.h
@@ -9,10 +9,10 @@
#import <UIKit/UIKit.h>
typedef enum {
- LRTableModelInsertEvent = 0,
- LRTableModelUpdateEvent,
- LRTableModelDeleteEvent,
- LRTableModelRefreshEvent
+ LRTableModelInsertRowEvent = 0,
+ LRTableModelUpdateRowEvent,
+ LRTableModelDeleteRowEvent,
+ LRTableModelRefreshDataEvent
} LRTableModelEventType;
@interface LRTableModelEvent : NSObject {
@@ -25,8 +25,8 @@ typedef enum {
- (id)initWithEventType:(LRTableModelEventType)eventType indexPath:(NSIndexPath *)theIndexPath;
- (BOOL)isEqualToEvent:(LRTableModelEvent *)otherEvent;
- (NSArray *)indexPaths;
-+ (id)insertionAtRow:(NSInteger)row;
-+ (id)updatedRow:(NSInteger)row;
-+ (id)deletedRow:(NSInteger)row;
-+ (id)refreshed;
++ (id)insertionAtRow:(NSInteger)row section:(NSInteger)section;
++ (id)updatedRow:(NSInteger)row section:(NSInteger)section;
++ (id)deletedRow:(NSInteger)row section:(NSInteger)section;
++ (id)refreshedData;
@end
View
38 Classes/LRTableModel/LRTableModelEvent.m
@@ -38,17 +38,17 @@ - (NSString *)description
{
NSString *eventType = nil;
switch (self.type) {
- case LRTableModelInsertEvent:
- eventType = @"LRTableModelInsertEvent";
+ case LRTableModelInsertRowEvent:
+ eventType = @"LRTableModelInsertRowEvent";
break;
- case LRTableModelUpdateEvent:
- eventType = @"LRTableModelUpdateEvent";
+ case LRTableModelUpdateRowEvent:
+ eventType = @"LRTableModelUpdateRowEvent";
break;
- case LRTableModelDeleteEvent:
- eventType = @"LRTableModelDeleteEvent";
+ case LRTableModelDeleteRowEvent:
+ eventType = @"LRTableModelDeleteRowEvent";
break;
- case LRTableModelRefreshEvent:
- eventType = @"LRTableModelRefreshEvent";
+ case LRTableModelRefreshDataEvent:
+ eventType = @"LRTableModelRefreshDataEvent";
break;
default:
eventType = @"UnknownEventType";
@@ -74,27 +74,27 @@ - (BOOL)isEqualToEvent:(LRTableModelEvent *)otherEvent
otherEvent.type == self.type;
}
-+ (id)insertionAtRow:(NSInteger)row;
++ (id)insertionAtRow:(NSInteger)row section:(NSInteger)section;
{
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
- return [[[self alloc] initWithEventType:LRTableModelInsertEvent indexPath:indexPath] autorelease];
+ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
+ return [[[self alloc] initWithEventType:LRTableModelInsertRowEvent indexPath:indexPath] autorelease];
}
-+ (id)updatedRow:(NSInteger)row;
++ (id)updatedRow:(NSInteger)row section:(NSInteger)section;
{
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
- return [[[self alloc] initWithEventType:LRTableModelUpdateEvent indexPath:indexPath] autorelease];
+ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
+ return [[[self alloc] initWithEventType:LRTableModelUpdateRowEvent indexPath:indexPath] autorelease];
}
-+ (id)deletedRow:(NSInteger)row;
++ (id)deletedRow:(NSInteger)row section:(NSInteger)section;
{
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
- return [[[self alloc] initWithEventType:LRTableModelDeleteEvent indexPath:indexPath] autorelease];
+ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
+ return [[[self alloc] initWithEventType:LRTableModelDeleteRowEvent indexPath:indexPath] autorelease];
}
-+ (id)refreshed;
++ (id)refreshedData;
{
- return [[[self alloc] initWithEventType:LRTableModelRefreshEvent indexPath:nil] autorelease];
+ return [[[self alloc] initWithEventType:LRTableModelRefreshDataEvent indexPath:nil] autorelease];
}
@end
View
12 Classes/SimpleTableModel/SimpleTableModel.m
@@ -49,42 +49,42 @@ - (NSArray *)sortedObjects
- (void)setSortOrder:(SortOrder)newSortOrder
{
sortOrder = newSortOrder;
- [self notifyListeners:[LRTableModelEvent refreshed]];
+ [self notifyListeners:[LRTableModelEvent refreshedData]];
}
- (void)addObject:(id)anObject;
{
[objects addObject:anObject];
NSInteger indexOfNewObject = [objects indexOfObject:anObject];
- [self notifyListeners:[LRTableModelEvent insertionAtRow:indexOfNewObject]];
+ [self notifyListeners:[LRTableModelEvent insertionAtRow:indexOfNewObject section:0]];
}
- (void)removeObject:(id)anObject;
{
NSInteger indexOfObject = [objects indexOfObject:anObject];
[objects removeObject:anObject];
- [self notifyListeners:[LRTableModelEvent deletedRow:indexOfObject]];
+ [self notifyListeners:[LRTableModelEvent deletedRow:indexOfObject section:0]];
}
- (void)replaceObjectAtIndex:(NSInteger)index withObject:(id)anObject;
{
[objects replaceObjectAtIndex:index withObject:anObject];
- [self notifyListeners:[LRTableModelEvent updatedRow:index]];
+ [self notifyListeners:[LRTableModelEvent updatedRow:index section:0]];
}
- (void)setObjects:(NSArray *)newObjects;
{
[objects removeAllObjects];
[objects setArray:newObjects];
- [self notifyListeners:[LRTableModelEvent refreshed]];
+ [self notifyListeners:[LRTableModelEvent refreshedData]];
}
- (void)insertObject:(id)anObject atIndex:(NSInteger)index;
{
[objects insertObject:anObject atIndex:index];
- [self notifyListeners:[LRTableModelEvent insertionAtRow:index]];
+ [self notifyListeners:[LRTableModelEvent insertionAtRow:index section:0]];
}
#pragma mark -
View
4 Classes/SimpleTableModel/SimpleTableViewController.m
@@ -100,10 +100,10 @@ - (void)sortOrderControlChanged:(UISegmentedControl *)control
- (void)tableModelChanged:(LRTableModelEvent *)changeEvent
{
switch (changeEvent.type) {
- case LRTableModelRefreshEvent:
+ case LRTableModelRefreshDataEvent:
[self.tableView reloadData];
break;
- case LRTableModelInsertEvent:
+ case LRTableModelInsertRowEvent:
[self.tableView insertRowsAtIndexPaths:changeEvent.indexPaths withRowAnimation:UITableViewRowAnimationTop];
break;
default:
View
8 Tests/Support/TestHelper.m
@@ -41,22 +41,22 @@ - (void)tableModelChanged:(LRTableModelEvent *)changeEvent {}
@end
id insertEventAtRow(int rowIndex) {
- return LRM_with(equalTo([LRTableModelEvent insertionAtRow:rowIndex]));
+ return LRM_with(equalTo([LRTableModelEvent insertionAtRow:rowIndex section:0]));
}
id anyEvent() {
return LRM_with(instanceOf([LRTableModelEvent class]));
}
id updateEventAtRow(int rowIndex) {
- return LRM_with(equalTo([LRTableModelEvent updatedRow:rowIndex]));
+ return LRM_with(equalTo([LRTableModelEvent updatedRow:rowIndex section:0]));
}
id deleteEventAtRow(int rowIndex) {
- return LRM_with(equalTo([LRTableModelEvent deletedRow:rowIndex]));
+ return LRM_with(equalTo([LRTableModelEvent deletedRow:rowIndex section:0]));
}
id refreshEvent() {
- return LRM_with(equalTo([LRTableModelEvent refreshed]));
+ return LRM_with(equalTo([LRTableModelEvent refreshedData]));
}

0 comments on commit 10523be

Please sign in to comment.
Something went wrong with that request. Please try again.