你有一个复杂的故事板uitableview界面,有很多自定义单元格。
当条件满足时,我需要删除一些单元格并添加其他单元格。
特别是,在第1节中,我最初有2个单元格,当满足条件时,我需要删除这两个单元格并添加一些单元格(例如代码中的3或4个单元格:self.viaggio.tragitto count
)。
这是我的代码:
- (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
得到:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(
0 CoreFoundation 0x00000001102caf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010ff63bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001101c301e -[__NSArrayI objectAtIndex:] + 190
3 UIKit 0x0000000111134912 -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 106
4 UIKit 0x0000000110cdf612 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1704
5 UIKit 0x0000000110c605ce +[UIView(Animation) performWithoutAnimation:] + 65
6 UIKit 0x0000000110cdef5b -[UITableView _configureCellForDisplay:forIndexPath:] + 312
7 UIKit 0x0000000110ce64cc -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 533
8 UIKit 0x0000000110cc5fb1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
9 UIKit 0x0000000110cdbe3c -[UITableView layoutSubviews] + 213
10 UIKit 0x0000000110c68973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
11 QuartzCore 0x0000000110a7ade8 -[CALayer layoutSublayers] + 150
12 QuartzCore 0x0000000110a6fa0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
13 QuartzCore 0x0000000110a6f87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14 QuartzCore 0x00000001109dd63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
15 QuartzCore 0x00000001109de74a _ZN2CA11Transaction6commitEv + 390
16 QuartzCore 0x00000001109dedb5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
17 CoreFoundation 0x00000001101ffdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18 CoreFoundation 0x00000001101ffd20 __CFRunLoopDoObservers + 368
19 CoreFoundation 0x00000001101f5b53 __CFRunLoopRun + 1123
20 CoreFoundation 0x00000001101f5486 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x0000000113c309f0 GSEventRunModal + 161
22 UIKit 0x0000000110bef420 UIApplicationMain + 1282
23 SeguiTreno 0x000000010f6afa03 main + 115
24 libdyld.dylib 0x00000001141bf145 start + 1
25 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我已经尝试过:
[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];
它仍然无法正常工作。
我尝试在故事板中添加单元格,但它确实有效!似乎我无法获得比故事板中的数字更多的单元格。 我错过了什么?