Ios 在静态UITableView上以编程方式添加单元格
Hi有一个复杂的故事板uitableview界面和许多自定义单元格。 当条件满足时,我需要删除一些单元格并添加其他单元格。 特别是,在第1节中,我最初有2个单元格,当条件满足时,我需要删除这两个单元格,并在代码中添加一些单元格,例如3或4个单元格:self.viaggio.tragitto count 这是我的密码:Ios 在静态UITableView上以编程方式添加单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,Hi有一个复杂的故事板uitableview界面和许多自定义单元格。 当条件满足时,我需要删除一些单元格并添加其他单元格。 特别是,在第1节中,我最初有2个单元格,当条件满足时,我需要删除这两个单元格,并在代码中添加一些单元格,例如3或4个单元格:self.viaggio.tragitto count 这是我的密码: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 2;
break;
case 1:
if(self.trenoCompilato) return [self.viaggio.tragitto count];
else return 2;
case 2:
if(self.trenoCompilato) return 1;
else return 0;
case 3:
return 1;
default:
return 0;
break;
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.section == 1 && self.trenoCompilato) {
static NSString *cellIdentifier = @"cellTragitto";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
Treno *trenoCell = self.viaggio.tragitto[indexPath.row];
cell.textLabel.text = [trenoCell stringaDescrizione];
cell.detailTextLabel.text = [[DateUtils shared] showHHmm:[[DateUtils shared] dateFrom:trenoCell.orarioPartenza]];
return cell;
}
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
显示故事板中不存在的新单元格的条件是布尔self.trenoCompilato
现在。当我执行[self.tableview reloadData]时,当布尔值为TRUE时,我得到:
我已经尝试过:
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationFade];
// inserisco cella con opzione per il cambio soluzione
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
但它仍然不起作用
我尝试在故事板中添加单元格,效果很好!似乎我无法获得比故事板中显示的数量更多的单元格。
我错过了什么 francesco289,我不确定,但它看起来像是您试图访问超出数组限制的对象,例如,如果您将数组大小设置为2,则表示您有[0..1]元素,这意味着如果您在tableView中指定:numberOfRowsInSection返回2,它将尝试访问0,1,2个索引。这可能会创建一个异常:和@Mike.R的可能副本。我的数组没有问题,所以我无法理解为什么它会创建一个异常…numberOfRowsInSection返回数组的计数,如果该数字小于或等于0,它就会工作等于故事板单元数。这可能是一个手机问题吗?我想答案是:
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationFade];
// inserisco cella con opzione per il cambio soluzione
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];