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?
Related
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)
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.
Im new to objective C and I was wondering if someone can help me figure out why my app keeps crashing. I believe it has something to do with my Array but I'm not sure how to fix it.. Can someone help please???..
Here's the Log output when it crashes:
2014-11-26 10:37:42.172 SamplePhotoReDo[89697:109203550] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010c75af35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c09cbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010c65301e -[__NSArrayI objectAtIndex:] + 190
3 UIKit 0x000000010d67474a -[UITableViewDataSource tableView:heightForHeaderInSection:] + 39
4 UIKit 0x000000010d221f4e -[UITableView _delegateWantsHeaderForSection:] + 261
5 UIKit 0x000000010d3a6983 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162
6 UIKit 0x000000010d3ace45 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 320
7 UIKit 0x000000010d3acf3a -[UITableViewRowData heightForTable] + 56
8 UIKit 0x000000010d1ffaf0 -[UITableView _updateContentSize] + 381
9 UIKit 0x000000010d21cecd -[UITableView didMoveToWindow] + 65
10 UIKit 0x000000010d1a39a0 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1482
11 UIKit 0x000000010d1b4333 -[UIScrollView _didMoveFromWindow:toWindow:] + 55
12 UIKit 0x000000010d1a368e -[UIView(Internal) _didMoveFromWindow:toWindow:] + 696
13 UIKit 0x000000010d19c112 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
14 UIKit 0x000000010d19c086 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
15 UIKit 0x000000010d1a5f4b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1604
16 UIKit 0x000000010d486534 -[UINavigationTransitionView transition:fromView:toView:] + 479
17 UIKit 0x000000010d2841ef -[UINavigationController _startTransition:fromViewController:toViewController:] + 2967
18 UIKit 0x000000010d284487 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
19 UIKit 0x000000010d284f47 -[UINavigationController __viewWillLayoutSubviews] + 43
20 UIKit 0x000000010d3ca509 -[UILayoutContainerView layoutSubviews] + 202
21 UIKit 0x000000010d1a8973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
22 QuartzCore 0x000000011052bde8 -[CALayer layoutSublayers] + 150
23 QuartzCore 0x0000000110520a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
24 QuartzCore 0x000000011052087e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
25 QuartzCore 0x000000011048e63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
26 QuartzCore 0x000000011048f74a _ZN2CA11Transaction6commitEv + 390
27 UIKit 0x000000010d12d54d -[UIApplication _reportMainSceneUpdateFinished:] + 44
28 UIKit 0x000000010d12e238 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642
29 UIKit 0x000000010d12cbf2 -[UIApplication workspaceDidEndTransaction:] + 179
30 FrontBoardServices 0x0000000112f002a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
31 CoreFoundation 0x000000010c69053c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
32 CoreFoundation 0x000000010c686285 __CFRunLoopDoBlocks + 341
33 CoreFoundation 0x000000010c686045 __CFRunLoopRun + 2389
34 CoreFoundation 0x000000010c685486 CFRunLoopRunSpecific + 470
35 UIKit 0x000000010d12c669 -[UIApplication _run] + 413
36 UIKit 0x000000010d12f420 UIApplicationMain + 1282
37 SamplePhotoReDo 0x000000010bb60453 main + 115
38 libdyld.dylib 0x000000010f2d2145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)'
Here's the code that might be causing the crash:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1 + self.collectionsFetchResults.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numberOfRows = 0;
if (section == 0) {
numberOfRows = 1; // "All Photos" section
} else {
PHFetchResult *fetchResult = self.collectionsFetchResults[section - 1];
numberOfRows = fetchResult.count;
}
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSString *localizedTitle = nil;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:AllPhotosReuseIdentifier forIndexPath:indexPath];
localizedTitle = NSLocalizedString(#"All Photos", #"");
} else {
cell = [tableView dequeueReusableCellWithIdentifier:CollectionCellReuseIdentifier forIndexPath:indexPath];
PHFetchResult *fetchResult = self.collectionsFetchResults[indexPath.section - 1];
PHCollection *collection = fetchResult[indexPath.row];
localizedTitle = collection.localizedTitle;
}
cell.textLabel.text = localizedTitle;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
if (section > 0) {
title = self.collectionsLocalizedTitles[section - 1];
}
return title;
}
#pragma mark - PHPhotoLibraryChangeObserver
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
// Call might come on any background queue. Re-dispatch to the main queue to handle it.
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *updatedCollectionsFetchResults = nil;
for (PHFetchResult *collectionsFetchResult in self.collectionsFetchResults) {
PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:collectionsFetchResult];
if (changeDetails) {
if (!updatedCollectionsFetchResults) {
updatedCollectionsFetchResults = [self.collectionsFetchResults mutableCopy];
}
[updatedCollectionsFetchResults replaceObjectAtIndex:[self.collectionsFetchResults indexOfObject:collectionsFetchResult] withObject:[changeDetails fetchResultAfterChanges]];
}
}
if (updatedCollectionsFetchResults) {
self.collectionsFetchResults = updatedCollectionsFetchResults;
[self.tableView reloadData];
}
});
}
reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
Quite clear, your array doesn't have as many elements as you think it has. Put a break point at the places where your array is populated and double check it's as expected.
I created a separate UITableViewCell and designed it using .xib file and it looks like
My header file for UITableViewCell looks like
#interface MenuTableViewCell : UITableViewCell
#property(nonatomic, weak) IBOutlet UIImageView *menuImage;
#property(nonatomic, weak) IBOutlet UILabel *menuLabel;
#end
and both properties are connected
I tried to use it in my MenuViewController as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.menuImage.image = [self getImageNameForRow:indexPath.row];
cell.menuLabel.text = self.features[(NSUInteger) indexPath.row];
return cell;
}
and
- (UIImage *)getImageNameForRow:(NSInteger)row {
if (row == 0) {
return [UIImage imageNamed:#"Transaction"];
}
if (row == 1) {
return [UIImage imageNamed:#"Summary"];
}
return [UIImage imageNamed:#"Budget"];
}
when I run my application, it crashes and shows the following on log
2014-08-30 14:51:54.387 pennyapp-ios[37988:70b] -[UITableViewCell menuImage]: unrecognized selector sent to instance 0x10bb1e5e0
2014-08-30 14:51:54.389 pennyapp-ios[37988:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell menuImage]: unrecognized selector sent to instance 0x10bb1e5e0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001023c6495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010212599e objc_exception_throw + 43
2 CoreFoundation 0x000000010245765d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001023b7d8d ___forwarding___ + 973
4 CoreFoundation 0x00000001023b7938 _CF_forwarding_prep_0 + 120
5 pennyapp-ios 0x00000001000044ed -[MenuTableViewController tableView:cellForRowAtIndexPath:] + 141
6 UIKit 0x0000000100904f8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
7 UIKit 0x00000001008ead5b -[UITableView _updateVisibleCellsNow:] + 2337
8 UIKit 0x00000001008fc721 -[UITableView layoutSubviews] + 207
9 UIKit 0x0000000100890993 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
10 QuartzCore 0x0000000104fbc802 -[CALayer layoutSublayers] + 151
11 QuartzCore 0x0000000104fb1369 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
12 QuartzCore 0x0000000104fb11ea _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
13 QuartzCore 0x0000000104f24fb8 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
14 QuartzCore 0x0000000104f26030 _ZN2CA11Transaction6commitEv + 394
15 UIKit 0x0000000100848e25 _afterCACommitHandler + 128
16 CoreFoundation 0x0000000102391dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
17 CoreFoundation 0x0000000102391d37 __CFRunLoopDoObservers + 391
18 CoreFoundation 0x0000000102371522 __CFRunLoopRun + 946
19 CoreFoundation 0x0000000102370d83 CFRunLoopRunSpecific + 467
20 GraphicsServices 0x00000001036e3f04 GSEventRunModal + 161
21 UIKit 0x0000000100830e33 UIApplicationMain + 1010
22 pennyapp-ios 0x0000000100001353 main + 115
23 libdyld.dylib 0x00000001033655fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I am very new to iOS and can't understand the error message, can some one please help me knowing what am In doing wrong?
There were 2 problems.
I was registering UITableViewCell instead of MenuTableViewCell
I was not registering the nib file I had
After making the change as following things, started to work again.
[self.tableView registerNib:[UINib nibWithNibName:#"MenuTableViewCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
Thank you for all your answers, they helped me a lot!
Declare an imgearray in interface,then add your array of images,next cellforatindexpath add code for UIImageView
yourcellname.yourimagename.image=[UIImage imagenamed:[yourimagearray objectAtIndexPath.row]];
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.