我构建了一个UITableView
字符串来自NSMutableArray
,但是当我上下滚动时,我的应用程序崩溃了。另一个问题是当我选择一行时它也会崩溃:它应该在NSMutableArray
中添加一个值。
对代码进行了评论。
代码:
@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];
}
}
此致 克劳迪奥
在myFile.h中:
@property(nonatomic, strong)NSMutableArray *daysArray;
@property(nonatomic, strong)NSMutableArray *activeDaysArray;
我不是在使用ARC!
以下是滚动视图时的崩溃日志:
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
以下是选择行时的崩溃日志:
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
答案 0 :(得分:1)
问题1:您应该以{{1}}和self.daysArray
访问您的数组。
问题2:indexPath.row
will return NSUInteger
是原始类型(self.activeDaysArray
)而不是unsigned long
类型。
通常Xcode会在您尝试此操作时发出警告
id
要将其插入数组,请执行此操作,
Incompatible integer to pointer conversion sending 'NSUInteger' (aka 'unsigned int') to parameter of type 'id'
在检查数组中的对象时遵循相同的步骤。
希望有所帮助!
答案 1 :(得分:0)
试试这个:
在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];
}
}