当我选择行时,我会像下面那样得到崩溃..请帮我解决
-[NSCFString count]: unrecognized selector sent to instance 0x596cce0
2011-03-10 13:53:39.449 MyTableApp[3959:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x596cce0'
*** Call stack at first throw:
(
0 CoreFoundation 0x02396919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x024e45de objc_exception_throw + 47
2 CoreFoundation 0x0239842b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x02308116 ___forwarding___ + 966
4 CoreFoundation 0x02307cd2 _CF_forwarding_prep_0 + 50
5 MyTableApp 0x000031c2 -[MainViewController tableView:numberOfRowsInSection:] + 53
6 UIKit 0x0046fa24 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
7 UIKit 0x004719c1 -[UITableViewRowData rectForFooterInSection:] + 108
8 UIKit 0x0047124d -[UITableViewRowData heightForTable] + 60
9 UIKit 0x00334596 -[UITableView(_UITableViewPrivate) _updateContentSize] + 333
10 UIKit 0x00323b7e -[UITableView noteNumberOfRowsChanged] + 123
11 UIKit 0x003301d2 -[UITableView reloadData] + 773
12 UIKit 0x05a778fc -[UITableViewAccessibility(Accessibility) reloadData] + 60
13 MyTableApp 0x00002c6d -[MainViewController tableView:didSelectRowAtIndexPath:] + 442
14 UIKit 0x0032c718 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
15 UIKit 0x00322ffe -[UITableView _userSelectRowAtIndexPath:] + 219
16 Foundation 0x00039cea __NSFireDelayedPerform + 441
17 CoreFoundation 0x02377d43 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
18 CoreFoundation 0x02379384 __CFRunLoopDoTimer + 1364
19 CoreFoundation 0x022d5d09 __CFRunLoopRun + 1817
20 CoreFoundation 0x022d5280 CFRunLoopRunSpecific + 208
21 CoreFoundation 0x022d51a1 CFRunLoopRunInMode + 97
22 GraphicsServices 0x02bfb2c8 GSEventRunModal + 217
23 GraphicsServices 0x02bfb38d GSEventRun + 115
24 UIKit 0x002c8b58 UIApplicationMain + 1160
25 MyTableApp 0x000024b8 main + 102
26 MyTableApp 0x00002449 start + 53
27 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
#import“MainViewController.h”
@implementation MainViewController
@synthesize selectedImage;
@synthesize unselectedImage;
@synthesize animals;
@synthesize selectedArray;
@synthesize inDeleteMode;
@synthesize deleteButton;
@synthesize mytableView;
-(void)doDelete:(id)sender
{
NSMutableArray *rowsToBeDeleted = [[NSMutableArray alloc] init];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
int index = 0;
for (NSNumber *rowSelected in selectedArray)
{
if ([rowSelected boolValue])
{
NSLog(@"%@ ssssssssssrow selected",rowSelected);
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"msmsmsmsmsssws");
NSLog(@"%i",indexPath.row);
[mytableView deselectRowAtIndexPath:indexPath animated:YES];
BOOL selected = [[selectedArray objectAtIndex:[indexPath row]] boolValue];
NSLog(@"ewrtwtqwrT");
if(selected == NO)
{
NSLog(@"cvbcvbNO BOOL value"); // ...
}
else {
NSLog(@"cvbvbYES BOOL VALURE");
}
[selectedArray replaceObjectAtIndex:[indexPath row] withObject:[NSNumber numberWithBool:!selected]];
[mytableView reloadData];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
animals = [NSMutableArray arrayWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil];
NSLog(@"%i snimals array count",[animals count]);
//selectedImage = [UIImage imageNamed:@"check_no.png"];
//unselectedImage = [UIImage imageNamed:@"check_yes.png"];
selectedImage = [UIImage imageNamed:@"selected.png"];
unselectedImage = [UIImage imageNamed:@"unselected.png"];
[self populateSelectedArray];
CGRect frameRect = CGRectMake(0,0,320,460);
mytableView = [[UITableView alloc] initWithFrame:frameRect style:UITableViewStyleGrouped];
mytableView.scrollEnabled = YES;
mytableView.delegate = self;
mytableView.dataSource = self;
self.view = mytableView;
deleteButton = [[UIButton alloc]init];
deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame = CGRectMake(0, 310,100, 45);
[deleteButton setTitle:@"Delete" forState:UIControlStateNormal];
[deleteButton addTarget:self action:@selector(doDelete:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:deleteButton];
[super viewDidLoad];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [animals count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Inside the tabkle view");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSLog(@"COming to the cell");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:kLabelRect];
label.tag = kCellLabelTag;
label.frame = CGRectMake(40.0, 12.0, 200.0, 20.0);
[cell.contentView addSubview:label];
[label release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:unselectedImage];
imageView.frame = CGRectMake(5.0, 10.0, 23.0, 23.0);
[cell.contentView addSubview:imageView];
imageView.tag = kCellImageViewTag;
[imageView release];
}
UILabel *label = (UILabel *)[cell.contentView viewWithTag:kCellLabelTag];
label.text = [animals objectAtIndex:[indexPath row]];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:kCellImageViewTag];
NSNumber *selected = [selectedArray objectAtIndex:[indexPath row]];
if([selected boolValue] == NO)
{
NSLog(@"NO BOOL value"); // ...
}
else {
NSLog(@"YES BOOL VALURE");
}
imageView.image = ([selected boolValue]) ? selectedImage : unselectedImage;
return cell;
}
- (void)populateSelectedArray
{
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[animals count]];
for (int i=0; i < [animals count]; i++)
[array addObject:[NSNumber numberWithBool:NO]];
self.selectedArray = array;
[array release];
}
//
// The textFieldShouldReturn method validates the text fields and display the message.
//
- ( BOOL ) textFieldShouldReturn: ( UITextField * ) theTextField
//-------------------------------------------------------------------
{
[theTextField resignFirstResponder];
return YES;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[animals release];
[selectedArray release];
[deleteButton release];
[selectedImage release];
[unselectedImage release];
[super dealloc];
}
@end
答案 0 :(得分:2)
我想会发生什么,
你的数组animal
是在numberOfRowsInSection
方法之前发布的某个地方,然后它的内存被一个字符串占用。
所以当你在numberOfRowsInSection
中调用count时会崩溃。因此,您需要使用调试指针手动检查它发生的位置。
更改此行以解决问题
animals = [NSMutableArray arrayWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil];
由此
animals = [[NSMutableArray arrayWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil] retain];
并以dealloc发布。
答案 1 :(得分:2)
你还没有分配动物阵列。
替换以下行
animals = [NSMutableArray arrayWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil];
使用
animals = [[NSMutableArray alloc] initWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil];
然后在dealloc方法中释放对象:意味着在dealloc方法中添加[animals release];
。
答案 2 :(得分:0)
看起来你正在某处调用NSString对象的count。您是否运行调试器以查看崩溃的确切代码行?看起来就像你的代码调用numberOfRowsInSection。
答案 3 :(得分:0)
检查.h文件中变量animals的数据类型。我认为它被宣布为NSString
。我这样说是因为如果你看一下错误信息的第一行就说了
-[NSCFString count]:
所以我认为这会导致numberOfRowsInSection
被调用时返回[animals count];
如果你想让animals
成为一个数组,你应该像前面的答案中提到的那样分配它。