NSInternalInconsistencyException(Invalid update: invalid number of rows in section 0) - ios

I design the process is that:
Enter into a CourseAddViewController(UITableViewController) via a Model Segue;
The right bar button display "Done"(At this status, the tableview display all data in eachrow with "-", but the additional row at the bottom with "+");
Press "Done" button, the save the data.
When I add the statement "self.editing = YES;" in - (void)viewDidLoad method, it will crash.
I found that it didn't perform "tableView:cellForRowAtIndexPath:" method.But, actually I trust that there is 1 row in section 0 via the result of NSLog().
If I don't add "self.editing = YES;", it run correctly.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.editing = YES; // here: adding it cause crash
}
After performing "insertRowsAtIndexPaths:withRowAnimation:", it crashed.
Two statement:
[self.tableView beginUpdates];
[self.tableView endUpdates];
add them or not, all cuase crash.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView beginUpdates];
NSInteger booksCount = self.course.books.count;
NSArray *booksInsertIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:booksCount inSection:0]];
if (editing) {
[self.tableView insertRowsAtIndexPaths:booksInsertIndexPath
withRowAnimation:UITableViewRowAnimationBottom]; // then, it crashes
} else {
[self.tableView deleteRowsAtIndexPaths:booksInsertIndexPath
withRowAnimation:UITableViewRowAnimationBottom];
}
[self.tableView endUpdates];
// ...
}
Other method,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"%d", self.fetchedResultsController.sections.count); // the result is always 1;
return self.fetchedResultsController.sections.count;
}
Other method,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
NSInteger rows = [sectionInfo numberOfObjects];
if (self.editing)
rows++;
NSLog(#"%d", rows); // the result is always 1;
return rows;
}
Other method,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:indexPath.section];
NSInteger rows = [sectionInfo numberOfObjects];
if (self.editing && rows == indexPath.row) {
return cell;
}
[self configureCell:cell atIndexPath:indexPath];
// NSLog(#"%d-%d-%d-%d", self.editing, rows, indexPath.section, indexPath.row);
return cell;
}
New Edit:
2012-10-27 08:44:29.934 Course Table[306:fb03] *** Assertion failure in
-[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/
UIKit-914.84/UITableView.m:1037
2012-10-27 08:44:29.935 Course Table[306:fb03] CRASH: Invalid update:
invalid number of rows in section 0. The number of rows contained in
an existing section after the update (1) must be equal to the number
of rows contained in that section before the update (1), plus or
minus the number of rows inserted or deleted from that section
(1 inserted, 0 deleted) and plus or minus the number of rows moved
into or out of that section (0 moved in, 0 moved out).
2012-10-27 08:44:29.942 Course Table[306:fb03] Stack Trace: (
0 CoreFoundation 0x016cf03e __exceptionPreprocess + 206
1 libobjc.A.dylib 0x01860cd6 objc_exception_throw + 44
2 CoreFoundation 0x01677a48 +[NSException raise:format:arguments:] + 136
3 Foundation 0x009d12cb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x000bf3d7 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 12439
5 UIKit 0x000ca6d2 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 295
6 UIKit 0x000ca711 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 55
7 Course Table 0x0000ef42 -[CTXCourseAddViewController setEditing:animated:] + 434
8 UIKit 0x0010de4c -[UIViewController(UINavigationControllerItem) setEditing:] + 49
9 Course Table 0x0000ec1f -[CTXCourseAddViewController viewDidLoad] + 655
10 UIKit 0x00101a1e -[UIViewController view] + 184
11 UIKit 0x00100fec -[UIViewController nextResponder] + 34
12 UIKit 0x00127f1d -[UIResponder _containsResponder:] + 40
13 UIKit 0x001121cb -[UINavigationController defaultFirstResponder] + 83
14 UIKit 0x00128df1 -[UIResponder(Internal) _deepestDefaultFirstResponder] + 36
15 UIKit 0x00128ea9 -[UIResponder(Internal) _promoteDeepestDefaultFirstResponder] + 36
16 UIKit 0x00322508 -[UIWindowController transitionViewDidStart:] + 89
17 UIKit 0x000df401 -[UITransitionView _didStartTransition] + 93
18 UIKit 0x000e008b -[UITransitionView transition:fromView:toView:] + 1169
19 UIKit 0x00321d6c -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 6114
20 UIKit 0x00108857 -[UIViewController presentViewController:withTransition:completion:] + 3579
21 UIKit 0x001089bc -[UIViewController presentViewController:animated:completion:] + 112
22 UIKit 0x001089fc -[UIViewController presentModalViewController:animated:] + 56
23 UIKit 0x00470f4a -[UIStoryboardModalSegue perform] + 250
24 UIKit 0x004654d0 -[UIStoryboardSegueTemplate perform:] + 174
25 CoreFoundation 0x016d0e99 -[NSObject performSelector:withObject:withObject:] + 73
26 UIKit 0x0003d14e -[UIApplication sendAction:to:from:forEvent:] + 96
27 UIKit 0x0027ba0e -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 145
28 CoreFoundation 0x016d0e99 -[NSObject performSelector:withObject:withObject:] + 73
29 UIKit 0x0003d14e -[UIApplication sendAction:to:from:forEvent:] + 96
30 UIKit 0x0003d0e6 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
31 UIKit 0x000e3ade -[UIControl sendAction:to:forEvent:] + 66
32 UIKit 0x000e3fa7 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
33 UIKit 0x000e3266 -[UIControl touchesEnded:withEvent:] + 549
34 UIKit 0x000623c0 -[UIWindow _sendTouchesForEvent:] + 513
35 UIKit 0x000625e6 -[UIWindow sendEvent:] + 273
36 UIKit 0x00048dc4 -[UIApplication sendEvent:] + 464
37 UIKit 0x0003c634 _UIApplicationHandleEvent + 8196
38 GraphicsServices 0x015b9ef5 PurpleEventCallback + 1274
39 CoreFoundation 0x016a3195 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
40 CoreFoundation 0x01607ff2 __CFRunLoopDoSource1 + 146
41 CoreFoundation 0x016068da __CFRunLoopRun + 2218
42 CoreFoundation 0x01605d84 CFRunLoopRunSpecific + 212
43 CoreFoundation 0x01605c9b CFRunLoopRunInMode + 123
44 GraphicsServices 0x015b87d8 GSEventRunModal + 190
45 GraphicsServices 0x015b888a GSEventRun + 103
46 UIKit 0x0003a626 UIApplicationMain + 1163
47 Course Table 0x000020dd main + 141
48 Course Table 0x00002045 start + 53
)
2012-10-27 08:44:29.945 Course Table[306:fb03] *** Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid
update: invalid number of rows in section 0. The number of rows contained
in an existing section after the update (1) must be equal to the number
of rows contained in that section before the update (1), plus or minus
the number of rows inserted or deleted from that section (1 inserted,
0 deleted) and plus or minus the number of rows moved into or out of that
section (0 moved in, 0 moved out).
terminate called throwing an exception(lldb)

Just add
[self.tableView reloadData];
before
self.editing = YES;
This is needed because the table view doesn't initially have information about its data source and delegate; if you create a table view, it always needs to be sent a reloadData message as part of its initialization.

Related

Row -1 or NSUIntegerMax Given by -[UITableViewDelegate tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:]

Under what circumstances do UITableView give -1 or NSUIntegerMax for the row in -[UITableViewDelegate tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:]?
Below it is a crash log form an user.
OS Version: iPhone OS 9.2.1 (13D15)
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 18446744073709551615 in section at index 0'
Last Exception Backtrace:
0 CoreFoundation 0x0000000181241900 __exceptionPreprocess + 124
1 libobjc.A.dylib 0x00000001808aff80 objc_exception_throw + 56
2 CoreData 0x0000000182cc50bc -[NSFetchedResultsController objectAtIndexPath:] + 456
3 My App 0x000000010022c678 -[MyViewController tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:] (MyViewController.m:560)
4 UIKit 0x0000000186145174 -[UITableView _titleForDeleteConfirmationButtonForRowAtIndexPath:] + 160
5 UIKit 0x00000001862756f4 -[UITableView _deleteConfirmationButtonForRowAtIndexPath:] + 116
6 UIKit 0x0000000186275cf4 -[UITableView _swipeActionButtonsForRowAtIndexPath:] + 824
7 UIKit 0x0000000186275998 -[UITableView _swipeActionButtons] + 92
8 UIKit 0x0000000186144bd8 __32-[UITableViewCell _beginSwiping]_block_invoke + 120
9 UIKit 0x0000000185f42964 +[UIView(Animation) performWithoutAnimation:] + 80
10 UIKit 0x0000000186144b48 -[UITableViewCell _beginSwiping] + 96
11 UIKit 0x0000000186272320 -[UITableViewWrapperView handleSwipeBeginning:] + 256
12 UIKit 0x00000001864b4ea4 _UIGestureRecognizerSendTargetActions + 396
13 UIKit 0x00000001860d85b8 _UIGestureRecognizerSendActions + 172
14 UIKit 0x0000000185f669b0 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 784
15 UIKit 0x00000001864b63bc ___UIGestureRecognizerUpdate_block_invoke904 + 72
16 UIKit 0x0000000185f25b58 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 372
17 UIKit 0x0000000185f228dc _UIGestureRecognizerUpdate + 2404
18 UIKit 0x0000000185f64820 -[UIWindow _sendGesturesForEvent:] + 1132
19 UIKit 0x0000000185f63e1c -[UIWindow sendEvent:] + 764
20 UIKit 0x0000000185f344cc -[UIApplication sendEvent:] + 248
21 UIKit 0x0000000185f32794 _UIApplicationHandleEventQueue + 5528
22 CoreFoundation 0x00000001811f8efc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
23 CoreFoundation 0x00000001811f8990 __CFRunLoopDoSources0 + 540
24 CoreFoundation 0x00000001811f6690 __CFRunLoopRun + 724
25 CoreFoundation 0x0000000181125680 CFRunLoopRunSpecific + 384
26 GraphicsServices 0x0000000182634088 GSEventRunModal + 180
27 UIKit 0x0000000185f9cd90 UIApplicationMain + 204
28 My App 0x00000001000a926c main (main.m:16)
29 ??? 0x0000000180cc68b8 0x0 + 0
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyMessage *message = [self messageAtIndex:indexPath.row];
if ([message.senderUid isEqualToString:mUid])
return NSLocalizedString(#"Delete", nil);
else
return NSLocalizedString(#"Hide", nil);
}
- (MyMessage *)messageAtIndex:(NSInteger)index
{
return [mMessagesController objectAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
}
indexPath.row and indexPath.section are each a NSUInteger (unsigned integer), which means that it's strictly positive. If you subtract and end up going negative, it jumps up to NSUIntegerMax. This makes it necessary to be careful with arithmetic involving index paths.
When you do row - 1 for row equal to zero, you will get NSUIntegerMax.
I guess the answer is: under no circumstance. If you're cell would be out of bounds the crash would display on rendering time, not when accessing [UITableViewDelegate tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:]
So the issue has to do with trying to retrieve an object from mMessagesController. Try using:
- (MyMessage *)messageAtIndex:(NSInteger)index
{
return [[mMessagesController fetchedObjects] objectAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
}
or
- (MyMessage *)messageAtIndex:(NSInteger)index
{
return [mMessagesController objectAtIndex:index];
}
In Addition you could also filter the call to messageAtIndex to avoid going out of bounds.
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < [mMessagesController fetchedObjects].count && indexPath.row >= 0) {
MyMessage *message = [self messageAtIndex:indexPath.row];
if ([message.senderUid isEqualToString:mUid])
return NSLocalizedString(#"Delete", nil);
else
return NSLocalizedString(#"Hide", nil);
} else {
return NSLocalizedString(#"Error", nil);
}
}
It is an iOS bug. Apple marked my bug as a duplicate of 23821434.

UItableview inside one of the UItableviewController static cell

I have a UITableviewController with 2 sections and each one have 1 custom cell. On the second static cell, I have dropped a UITableview and implemented the delegates accordingly.
Code snippet.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.activityTable) { // dynamic table
return 1;
}else {
return 2;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.activityTable) { // dynamic tableview
return [self.fetchActivityController.fetchedObjects count];
}else {
return 1; //static tableview
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.activityTable) {
if ([self.fetchActivityController.fetchedObjects count] == 0)
return nil;
ADNetworkCell *cell=[self.activityTable dequeueReusableCellWithIdentifier:NSStringFromClass([ADNetworkCell class])];
ADActivity* activityObj = (ADActivity*)[self.fetchActivityController.fetchedObjects objectAtIndex:indexPath.row];
ADUser* nUser = [activityObj networkAgent];
[cell configWithAgent:nUser withActivity:[activityObj activity]];
cell.activityText.delegate=self;
return cell;
}else { // static tableview code
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.activityTable) {
return 95.0f;
} else {
// The height of the 2nd static cell should be size of tableview with dynamic cell.
if (indexPath.section == 1) {
return 95 * [self.activityArray count];
}
}
return 44.0f;
}
Its crashing
uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI
objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack: ( 0 CoreFoundation 0x000000010d0b8f45 exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c60bdeb objc_exception_throw + 48
2 CoreFoundation 0x000000010cfa7b14 -[__NSArrayI objectAtIndex:] + 164
3 UIKit 0x000000010b179516 -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 160
4 UIKit 0x000000010ae4929b -[UITableViewController tableView:indentationLevelForRowAtIndexPath:] + 65
5 UIKit 0x000000010abb7325 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 2226
6 UIKit 0x000000010ab15c10 +[UIView(Animation) performWithoutAnimation:] + 65
7 UIKit 0x000000010abb6a5a -[UITableView _configureCellForDisplay:forIndexPath:] + 475
8 UIKit 0x000000010abc1e58 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 828
9 UIKit 0x000000010abc1f3f -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
10 UIKit 0x000000010ab97307 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187
11 UIKit 0x000000010abcad1c -[UITableView _performWithCachedTraitCollection:] + 92
12 UIKit 0x000000010abb2884 -[UITableView layoutSubviews] + 223
13 UIKit 0x000000010ab20e40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
14 QuartzCore 0x000000010a7ec59a -[CALayer layoutSublayers] + 146
15 QuartzCore 0x000000010a7e0e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
16 QuartzCore 0x000000010a7e0cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
17 QuartzCore 0x000000010a7d5475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
18 QuartzCore 0x000000010a802c0a _ZN2CA11Transaction6commitEv + 486
19 UIKit 0x000000010aa66ca4 _UIApplicationHandleEventQueue + 7329
20 CoreFoundation 0x000000010cfe5011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
21 CoreFoundation 0x000000010cfdaf3c __CFRunLoopDoSources0 + 556
22 CoreFoundation 0x000000010cfda3f3 __CFRunLoopRun + 867
23 CoreFoundation 0x000000010cfd9e08 CFRunLoopRunSpecific + 488
24 GraphicsServices 0x000000010e733ad2 GSEventRunModal + 161
25 UIKit 0x000000010aa6c30d UIApplicationMain + 171
26 Agentdesks 0x000000010863164f main + 111
27 libdyld.dylib 0x000000010d59c92d start + 1 28 ???
0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with
uncaught exception of type NSException (lldb)

TableView in a tableView chrashes

I have a static UItableView. In one of the cells, I have a dynamic prototype UITableView. Here is the code I implemented:
In viewDidLoad:
self.tagsArray = [[NSArray alloc] initWithObjects:#"hey", #"what", #"ola", #"dada", #"hoster", #"umi", nil];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.tableView == tableView ? 2 : 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.tableView == tableView)
return (section == 0) ? 3 : 2;
else
return [self.tagsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (self.tableView == tableView) {
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell.textLabel setText:self.tagsArray [indexPath.row]];
}
return cell;
}
When I run the app, everything works fine. But when I start scrolling on the inner tableView (the dynamic prototype one), then I get the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
*** First throw call stack:
(
0 CoreFoundation 0x00000001012a7c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000100f40bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010119e17e -[__NSArrayI objectAtIndex:] + 190
3 UIKit 0x0000000101e12132 -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 106
4 UIKit 0x00000001019dc1f9 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1711
5 UIKit 0x000000010195a68e +[UIView(Animation) performWithoutAnimation:] + 65
6 UIKit 0x00000001019dbb3b -[UITableView _configureCellForDisplay:forIndexPath:] + 312
7 UIKit 0x00000001019e3a41 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 533
8 UIKit 0x00000001019c2248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
9 UIKit 0x00000001019d88a9 -[UITableView layoutSubviews] + 210
10 UIKit 0x0000000101962a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
11 QuartzCore 0x0000000100934ec2 -[CALayer layoutSublayers] + 146
12 QuartzCore 0x00000001009296d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
13 QuartzCore 0x0000000100929546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14 QuartzCore 0x0000000100895886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
15 QuartzCore 0x0000000100896a3a _ZN2CA11Transaction6commitEv + 462
16 QuartzCore 0x00000001008970eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
17 CoreFoundation 0x00000001011daca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18 CoreFoundation 0x00000001011dac00 __CFRunLoopDoObservers + 368
19 CoreFoundation 0x00000001011d0a33 __CFRunLoopRun + 1123
20 CoreFoundation 0x00000001011d0366 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x000000010510da3e GSEventRunModal + 161
22 UIKit 0x00000001018e2900 UIApplicationMain + 1282
23 myApp 0x00000001004cf51f main + 111
24 libdyld.dylib 0x0000000102cfe145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I don't know what the above error means, but I think that it's not creating new cells for some reason, and because of that, it has a conflict with the number of cells to display, so it crashes with the above error.
Why isn't the inner tableView creating new cells, and what can I do to fix it?
What the error is telling you is that you tried to access an element that doesn't exist.
Your data array only has 2 elements, but you are hardcoding the amount of cells you want to create (3) so when it looks for the 3rd element to create the cell it crashes because is not existent.

Adding cells programmatically on a static UITableView

Hi have a complex storyboard uitableview interface with a lot of custom cells.
When a condition is satisfied I need to remove some cells and add others.
In particular, in section 1 I have initially 2 cells and when the condition is satisfied I need to remove this two cells and add some cells (for instance 3 or 4 cells, in the code: self.viaggio.tragitto count).
Here is my code:
- (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];
}
The condition to display the new cells (they are NOT present in the storyboard) is the boolean self.trenoCompilato
Now. When I do [self.tableview reloadData] when the boolean is TRUE i get:
*** 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
I have already tried with:
[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];
and it still doesn't work.
I tried to add cells in the storyboard and it works! It seems that I can't get more cells than the number is present in the storyboard.
What I'm missing?

Unable to dequeue a cell with identifier [duplicate]

This question already has answers here:
Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:
(22 answers)
Closed 8 years ago.
here's the method causing the problem:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
// Configure the cell...
int row = [indexPath row];
Books *book = [myBooks objectAtIndex:row];
cell.bookName.text = book.bookName;
return cell;
}
full log:
2014-02-05 23:19:09.458 Books[1425:a0b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:5251
2014-02-05 23:19:09.471 Books[1425:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
0 CoreFoundation 0x017395e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014bc8b6 objc_exception_throw + 44
2 CoreFoundation 0x01739448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x0109d23e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x003135e3 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 170
5 Books 0x000032df -[TableViewController tableView:cellForRowAtIndexPath:] + 127
6 UIKit 0x0031dd2f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
7 UIKit 0x0031de03 -[UITableView _createPreparedCellForGlobalRow:] + 69
8 UIKit 0x00302124 -[UITableView _updateVisibleCellsNow:] + 2378
9 UIKit 0x003155a5 -[UITableView layoutSubviews] + 213
10 UIKit 0x00299dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11 libobjc.A.dylib 0x014ce81f -[NSObject performSelector:withObject:] + 70
12 QuartzCore 0x03af372a -[CALayer layoutSublayers] + 148
13 QuartzCore 0x03ae7514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x03af3675 -[CALayer layoutIfNeeded] + 160
15 UIKit 0x00354ca3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
16 UIKit 0x00273d27 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
17 UIKit 0x002728c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
18 UIKit 0x00272798 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
19 UIKit 0x00272820 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
20 UIKit 0x002718ba __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
21 UIKit 0x0027181c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
22 UIKit 0x00272573 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
23 UIKit 0x00275b66 -[UIWindow setDelegate:] + 449
24 UIKit 0x00346dc7 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
25 UIKit 0x0026b7cc -[UIWindow addRootViewControllerViewIfPossible] + 609
26 UIKit 0x0026b947 -[UIWindow _setHidden:forced:] + 312
27 UIKit 0x0026bbdd -[UIWindow _orderFrontWithoutMakingKey] + 49
28 UIKit 0x0027644a -[UIWindow makeKeyAndVisible] + 65
29 UIKit 0x002298e0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
30 UIKit 0x0022dfb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
31 UIKit 0x0024242c -[UIApplication handleEvent:withNewEvent:] + 3447
32 UIKit 0x00242999 -[UIApplication sendEvent:] + 85
33 UIKit 0x0022fc35 _UIApplicationHandleEvent + 736
34 GraphicsServices 0x0368c2eb _PurpleEventCallback + 776
35 GraphicsServices 0x0368bdf6 PurpleEventCallback + 46
36 CoreFoundation 0x016b4dd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
37 CoreFoundation 0x016b4b0b __CFRunLoopDoSource1 + 523
38 CoreFoundation 0x016df7ec __CFRunLoopRun + 2156
39 CoreFoundation 0x016deb33 CFRunLoopRunSpecific + 467
40 CoreFoundation 0x016de94b CFRunLoopRunInMode + 123
41 UIKit 0x0022d6ed -[UIApplication _run] + 840
42 UIKit 0x0022f94b UIApplicationMain + 1225
43 Books 0x000038bd main + 141
44 libdyld.dylib 0x01d75725 start + 0
45 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
You can directly set the identifier using the storyboard like this.
From the documentation:
Important: You must register a class or nib file using the
registerNib:forCellReuseIdentifier: or
registerClass:forCellReuseIdentifier: method before calling this
method.
You need to register the cell as a usable class for that identifier. Call this after you initialize your UITableView (probably in viewDidLoad)
[self.tableView registerClass:[TableCell class] forCellReuseIdentifier:CellIdentifier]
This assumes CellIdentifier has been moved somewhere where it can be accessed from here.
Try this:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
int row = [indexPath row];
Books *book = [myBooks objectAtIndex:row];
cell.bookName.text = book.bookName;
return cell;
}

Resources