/ / Riordina UITableview con celle statiche in Code - iOS, Object-C, uitableview

Riordina UITableview con celle statiche in Code-ios, objective-c, uitableview

Ho una UITableview con 7 celle statiche e ciascunaCell ha un seguito per un'altra vista. Voglio rendere riordinabili le celle. Sto scrivendo il reuseID e la posizione di ciascuna cella in NSUserdefaults dopo che l'utente ha riordinato le celle.

Ma come posso dire a Tableview dove Cell deve essere visualizzata quando la vista viene (ri) caricata.

I migliori saluti

pugnale

risposte:

3 per risposta № 1

Normalmente, quando si utilizza una vista tabella statica, l'utentenon implementerebbe i metodi dell'origine dati, ma in questo caso sembra necessario farlo. Ho creato un IBOutletCollection e aggiunto le mie celle a quell'array (le ho aggiunte in ordine dalla prima cella all'ultima, in modo che appaiano nell'ordine storyboard la prima volta che la tabella viene caricata. In cellForRowAtIndexPath non è possibile annullare la dequezione di una cella, in quanto non funziona per le celle statiche, quindi ottengo la cella dalla raccolta outlet. Ho un array separato che mantiene traccia dell'ordine in cui dovrebbero apparire le celle, ed è quello che salvo ai valori predefiniti dell'utente. Ecco il codice che ha funzionato nei miei test,

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