/ /コードの静的セルでUITableviewを並べ替える-ios、objective-c、uitableview

コードで静的なセルを使ってUITableviewを並べ替える - ios、objective-c、uitableview

7つの静的セルとそれぞれのUITableviewがありますセルには他のビューへのセグエがあります。 Cellsを並べ替え可能にしたい。 ユーザーがセルを並べ替えた後、再利用IDと各セルの位置をNSUserdefaultsに書き込みます。

しかし、ビューが(再)ロードされたときにCellを表示する必要がある場所をTableviewに伝えるにはどうすればよいですか?

宜しくお願いします

ダーク

回答:

回答№1の場合は3

通常、静的テーブルビューを使用する場合、データソースメソッドを実装しませんでしたが、この場合は実装する必要があるようです。IBOutletCollectionを作成し、その配列にセルを追加しました(最初のセルから最後のセルまで順番に追加したため、 cellForRowAtIndexPathでは、セルをデキューできません。静的セルでは機能しないため、代わりに、アウトレットコレクションからセルを取得します。セルが表示される順序を追跡し、それをユーザーのデフォルトに保存します。ここに、テストで機能したコードを示します。

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