Normally, when using a static table view, you wouldn't implement the data source methods, but in this case it seems necessary to do so. I created an IBOutletCollection, and added my cells to that array (I added them in order from the first cell to the last, so they would appear in the storyboard order the first time the table loads). In cellForRowAtIndexPath you can't dequeue a cell, as that doesn't work for static cells so instead, I get the cell from the outlet collection. I have a separate array that keeps track of the order that the cells should appear, and that is what I save to the user defaults. Here is the code that worked in my tests,
@interface StaticTableViewController ()
@property (strong,nonatomic) NSMutableArray *cells;
@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *tableCells;
@end
@implementation StaticTableViewController
-(void)viewDidLoad {
[super viewDidLoad];
self.cells = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"cells"] mutableCopy];
if (! self.cells) self.cells = [@[@0,@1,@2,@3,@4] mutableCopy];
}
- (IBAction)enableReordering:(UIBarButtonItem *)sender {
[self.tableView setEditing:YES animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cells.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger idx = [self.cells[indexPath.row] integerValue];
UITableViewCell *cell = self.tableCells[idx];
return cell;
}
-(BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSNumber *numberToMove = self.cells[fromIndexPath.row];
[self.cells removeObjectAtIndex:fromIndexPath.row];
[self.cells insertObject:numberToMove atIndex:toIndexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:self.cells forKey:@"cells"];
[[NSUserDefaults standardUserDefaults] synchronize];
}