この場合、UITableView の再利用性は役に立ちませんが (ほとんどの場合、再利用性はもちろん素晴らしいことです)、編集を保存するのは非常に困難です。そのため、再利用を避けることができ、事前に細胞を準備できます。
NSMutableArray
ViewController にiVar またはプロパティを追加する
@property (nonatomic, strong) NSMutableArray *cells;
あなたのviewDidLoadで:cells
何もせずに準備してくださいreuseIdentifier
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Creates tableView cells.
[self createCells];
}
- (void)createCells
{
self.cells = [NSMutableArray array];
TCTimeCell *cellCallTime = [[TCTimeCell alloc] initWithTitle:@"CALL" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeCall];
[_cells addObject:cellCallTime];
TCTimeCell *cellLunchOut = [[TCTimeCell alloc] initWithTitle:@"LUNCH START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchOut];
[_cells addObject:cellLunchOut];
TCTimeCell *cellLunchIn = [[TCTimeCell alloc] initWithTitle:@"LUNCH END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchIn];
[_cells addObject:cellLunchIn];
TCTimeCell *cellSecondMealOut = [[TCTimeCell alloc] initWithTitle:@"2ND MEAL START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealOut];
[_cells addObject:cellSecondMealOut];
TCTimeCell *cellSecondMealIn = [[TCTimeCell alloc] initWithTitle:@"2ND MEAL END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealIn];
[_cells addObject:cellSecondMealIn];
TCTimeCell *cellWrapTime = [[TCTimeCell alloc] initWithTitle:@"WRAP" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeWrap];
[_cells addObject:cellWrapTime];
}
この配列から tableView を設定できます。
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.cells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.cells[indexPath.row];
}
セクション化された tableView がある場合は、セルを として準備できますarray of arrays
。その場合、データ ソース メソッドは以下のようになります。
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return [self.cells count];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.cells[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.cells[indexPath.section][indexPath.row];
}