在这种情况下,UITableView 的可重用性对您没有帮助(在大多数情况下,可重用性当然是一件好事),但在保留编辑时会遇到太多困难。因此,您可以避免重复使用并提前准备好您的细胞。
在 ViewController 中添加NSMutableArray
iVar 或属性
@property (nonatomic, strong) NSMutableArray *cells;
在你看来DidLoad:准备你的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];
}