/ / Reordene o UITableview com células estáticas no código - ios, objetivo-c, uitableview

Reordenar UITableview com células estáticas em Code - ios, objective-c, uitableview

Eu tenho uma UITableview com 7 células estáticas e cada umaA célula possui um segue para uma outra visualização. Quero tornar as células reordenadas. Estou escrevendo o reuseID e a posição de cada célula no NSUserdefaults após o usuário reordenar as células.

Mas como posso dizer ao Tableview onde o Cell precisa ser exibido quando a visualização é (re) carregada.

Cumprimentos

Dirk

Respostas:

3 para resposta № 1

Normalmente, ao usar uma exibição de tabela estática, vocênão implementaria os métodos da fonte de dados, mas, neste caso, parece necessário fazê-lo. Criei um IBOutletCollection e adicionei minhas células a essa matriz (adicionei-as em ordem da primeira célula à última, para que aparecessem na ordem do storyboard na primeira vez em que a tabela é carregada.) Em cellForRowAtIndexPath, você não pode remover a fila de uma célula, pois isso não funciona para células estáticas. Em vez disso, recebo a célula da coleção de tomadas. Tenho uma matriz separada que mantém acompanhar a ordem em que as células devem aparecer, e é isso que eu salvo nos padrões do usuário. Aqui está o código que funcionou em meus testes,

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