Reputation: 2037
I have built a UITableView
with strings that come from a NSMutableArray
but my app is crashing when I scroll it up and down. Another problem is that it's crashing too when I select a row: it's supposed to add a value into a NSMutableArray
.
Points of crash is commented on the code.
Code:
@synthesize daysArray, activeDaysArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
daysArray = [NSMutableArray arrayWithObjects:@"Mondays",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturdays", @"Sundays", nil];
activeDaysArray = [[NSMutableArray alloc]initWithCapacity:0];
}
return self;
}
#pragma Table View Delegates and DataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//This point is OK, it's returning 7 as it is supposed too
return [daysArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [daysArray objectAtIndex:indexPath.row]; //Crashing here: EXC_BAD_ACCESS;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* myCell = [tableView
cellForRowAtIndexPath:indexPath];
if ([myCell accessoryType] == UITableViewCellAccessoryNone) {
[myCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[activeDaysArray addObject:indexPath.row]; //Crashing here: EXC_BAD_ACCESS
}else{
[myCell setAccessoryType:UITableViewCellAccessoryNone];
int indexOfObject = [activeDaysArray indexOfObject:indexPath.row];
[activeDaysArray removeObjectAtIndex:indexOfObject];
}
}
Regards, Claudio
In myFile.h:
@property(nonatomic, strong)NSMutableArray *daysArray;
@property(nonatomic, strong)NSMutableArray *activeDaysArray;
I am NOT USING ARC!
Here is the crash log for the case when I scroll the view:
Last Exception Backtrace:
0 CoreFoundation 0x2e25ff4e __exceptionPreprocess + 126
1 libobjc.A.dylib 0x386386aa objc_exception_throw + 34
2 CoreFoundation 0x2e2638e2 -[NSObject(NSObject) doesNotRecognizeSelector:] + 198
3 CoreFoundation 0x2e2621ce ___forwarding___ + 702
4 CoreFoundation 0x2e1b1594 __forwarding_prep_0___ + 20
5 Exame_iPhone 0x00109dae 0xe9000 + 134574
6 UIKit 0x30b0347e -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 406
7 UIKit 0x30aab6ca -[UITableView _updateVisibleCellsNow:] + 1650
8 UIKit 0x30aaaf7c -[UITableView layoutSubviews] + 180
9 UIKit 0x309d101e -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 342
10 QuartzCore 0x3065a246 -[CALayer layoutSublayers] + 138
11 QuartzCore 0x30655a56 CA::Layer::layout_if_needed(CA::Transaction*) + 346
12 QuartzCore 0x306558e8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 12
13 QuartzCore 0x306552fa CA::Context::commit_transaction(CA::Transaction*) + 226
14 QuartzCore 0x3065510a CA::Transaction::commit() + 310
15 QuartzCore 0x306a9d80 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 512
16 IOMobileFramebuffer 0x332ce768 IOMobileFramebufferVsyncNotifyFunc + 100
17 IOKit 0x2ef40d00 IODispatchCalloutFromCFMessage + 244
18 CoreFoundation 0x2e21fe24 __CFMachPortPerform + 132
19 CoreFoundation 0x2e22a9e2 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 30
20 CoreFoundation 0x2e22a97e __CFRunLoopDoSource1 + 342
21 CoreFoundation 0x2e229152 __CFRunLoopRun + 1394
22 CoreFoundation 0x2e193ce2 CFRunLoopRunSpecific + 518
23 CoreFoundation 0x2e193ac6 CFRunLoopRunInMode + 102
24 GraphicsServices 0x32eb427e GSEventRunModal + 134
25 UIKit 0x30a35a3c UIApplicationMain + 1132
26 Exame_iPhone 0x000ee920 0xe9000 + 22816
27 libdyld.dylib 0x38b40ab2 tlv_initializer + 2
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x38bf71fc __pthread_kill + 8
1 libsystem_pthread.dylib 0x38c60a2e pthread_kill + 54
2 libsystem_c.dylib 0x38ba7ff8 abort + 72
3 libc++abi.dylib 0x37ed6cd2 abort_message + 70
4 libc++abi.dylib 0x37eef6e0 default_terminate_handler() + 248
5 libobjc.A.dylib 0x3863891e _objc_terminate() + 190
6 libc++abi.dylib 0x37eed1c4 std::__terminate(void (*)()) + 76
7 libc++abi.dylib 0x37eecd28 __cxa_rethrow + 96
8 libobjc.A.dylib 0x386387f2 objc_exception_rethrow + 38
9 CoreFoundation 0x2e193d58 CFRunLoopRunSpecific + 636
10 CoreFoundation 0x2e193ac6 CFRunLoopRunInMode + 102
11 GraphicsServices 0x32eb427e GSEventRunModal + 134
12 UIKit 0x30a35a3c UIApplicationMain + 1132
13 Exame_iPhone 0x000ee920 0xe9000 + 22816
14 libdyld.dylib 0x38b40ab4 start + 0
and here is the crash log when I select a row:
Thread 0 Crashed:
0 CoreFoundation 0x2e18e04c CFRetain + 8
1 CoreFoundation 0x2e199c42 -[__NSArrayM insertObject:atIndex:] + 502
2 Exame_iPhone 0x00099554 -[CreateAlarmViewController tableView:didSelectRowAtIndexPath:] (CreateAlarmViewController.m:117)
3 UIKit 0x30aee326 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1074
4 UIKit 0x30ba124e -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 210
5 UIKit 0x30a5196e _applyBlockToCFArrayCopiedToStack + 314
6 UIKit 0x309c946e _afterCACommitHandler + 426
7 CoreFoundation 0x2e22b1d2 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
8 CoreFoundation 0x2e228b74 __CFRunLoopDoObservers + 280
9 CoreFoundation 0x2e228eb6 __CFRunLoopRun + 726
10 CoreFoundation 0x2e193ce2 CFRunLoopRunSpecific + 518
11 CoreFoundation 0x2e193ac6 CFRunLoopRunInMode + 102
12 GraphicsServices 0x32eb427e GSEventRunModal + 134
13 UIKit 0x30a35a3c UIApplicationMain + 1132
14 Exame_iPhone 0x0007def0 main (main.m:14)
15 libdyld.dylib 0x38b40ab4 start + 0
Upvotes: 1
Views: 1395
Reputation: 13222
Issue 1: You should access your arrays as self.daysArray
and self.activeDaysArray
.
Issue 2: indexPath.row
will return NSUInteger
which is primitive type (unsigned long
)and not id
type.
Usually Xcode will give warning when you try this
Incompatible integer to pointer conversion sending 'NSUInteger' (aka 'unsigned int') to parameter of type 'id'
To insert it into your array, do this,
NSNumber *row = [NSNumber numberWithInt:indexPath.row];
self.activeDaysArray addObject:row];
Follow same while checking for object in your array.
Hope that helps!
Upvotes: 1
Reputation: 29
Try This:
In myFile.h:
@property(nonatomic, strong)NSMutableArray *daysArray;
@property(nonatomic, strong)NSMutableArray *activeDaysArray;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//This point is OK, it's returning 7 as it is supposed too
return [self.daysArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// if (indexPath.row < self.daysArray.count){
cell.textLabel.text = [self.daysArray objectAtIndex:indexPath.row];
// }
//Crashing here: EXC_BAD_ACCESS;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* myCell = [tableView
cellForRowAtIndexPath:indexPath];
if ([myCell accessoryType] == UITableViewCellAccessoryNone) {
[myCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[self.activeDaysArray addObject:indexPath.row]; //Crashing here: EXC_BAD_ACCESS
}else{
[myCell setAccessoryType:UITableViewCellAccessoryNone];
int indexOfObject = [self.activeDaysArray indexOfObject:indexPath.row];
[self.activeDaysArray removeObjectAtIndex:indexOfObject];
}
}
Upvotes: 0