1
votes

I have a UITableview with 7 Static Cells and each Cell has a segue to an other View. I want to make the Cells reorder-able. I'm writing the reuseID and position of each Cell into NSUserdefaults after the user reordered the Cells.

But how can i tell the Tableview where wich Cell needs to be displaye when the view is (re)loaded.

Best regards

Dirk

1
Have you gotten the reordering to work at all (I've never tried it with static cells, so I don't know if that's possible)? Is the problem only how to make the table see your new order when you relaunch? - rdelmar
The reordering works while the tableview is showing, but when i leave the view and come back, the standard ordering is applied. I'm looking for a method in Software to reorder the static cells before the tableview is shown. - Dirk

1 Answers

4
votes

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];
}