-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableObjectArrayTableViewController.m
More file actions
111 lines (94 loc) · 3.68 KB
/
EditableObjectArrayTableViewController.m
File metadata and controls
111 lines (94 loc) · 3.68 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
//
// EditableObjectArrayTableViewController.m
// xolawareUI
//
// Created by kb on 2012.11.10.
#import "xolawareOpenSourceCopyright.h"
#import "EditableObjectArrayTableViewController.h"
@implementation EditableObjectArrayTableViewController
//- (void)setObjects:(NSArray*)newObjects {
// int delta;
// __weak NSArray* _objects = _editableObjectsDataSource.editableObjects;
// NSUInteger newCount = newObjects.count;
// NSUInteger oldCount = _objects.count;
// if (_objects && newObjects && 0 < (delta = newCount - oldCount))
// {
// NSArray* newObjects1N = [newObjects subarrayWithRange:NSMakeRange(0, oldCount)];
// if ([newObjects1N isEqualToArray:_objects])
// {
// // update tableView
// NSMutableArray* newIndexPaths = [NSMutableArray arrayWithCapacity:delta];
// for (int i = oldCount; i < newCount ; ++i)
// [newIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
//
// [self.tableView beginUpdates];
// [[self editableObjectsDataSource] setEditableObjects:newObjects];
// [self.tableView insertRowsAtIndexPaths:newIndexPaths
// withRowAnimation:UITableViewRowAnimationBottom];
// [self.tableView endUpdates];
//
// return;
// }
// }
//
// [self.editableObjectsDataSource setEditableObjects:newObjects];
//
// if (!self.isEditing)
// [self.tableView reloadData];
//}
#pragma mark - public implementation
- (void)removeObjectFromObjects:(id)object {
NSMutableArray* objectsMinusThis = _editableObjectsDataSource.editableObjects.mutableCopy;
[objectsMinusThis removeObject:object];
[_editableObjectsDataSource setEditableObjects:objectsMinusThis.copy];
}
- (void)removeObjectFromObjectsAtIndex:(NSUInteger)index {
NSMutableArray* objectsMinusThis = _editableObjectsDataSource.editableObjects.mutableCopy;
[objectsMinusThis removeObjectAtIndex:index];
[_editableObjectsDataSource setEditableObjects:objectsMinusThis.copy];
}
#pragma mark - view controller life cycle overrides
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
// if (editing && self.tableView.indexPathForSelectedRow)
// self.rowPriorToEditing = self.tableView.indexPathForSelectedRow.row;
// else
// self.rowPriorToEditing = NSNotFound;
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; // our number of sections for a simple array of objects
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _editableObjectsDataSource.editableObjects.count;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void) tableView:(UITableView*)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath*)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self removeObjectFromObjectsAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
-(void) tableView:(UITableView*)tableView
moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath
toIndexPath:(NSIndexPath*)destinationIndexPath
{
NSMutableArray* reorderedObjects = _editableObjectsDataSource.editableObjects.mutableCopy;
NSObject* objectToMove = [reorderedObjects objectAtIndex:sourceIndexPath.row];
[reorderedObjects removeObjectAtIndex:sourceIndexPath.row];
[reorderedObjects insertObject:objectToMove atIndex:destinationIndexPath.row];
[_editableObjectsDataSource setEditableObjects:reorderedObjects.copy];
}
@end