2011-03-10 81 views
0

當我選擇該行,我得到像下面的崩潰..pls幫助我幫助有裏面的tableview複選框

-[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' 

#進口「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 

回答

2

我想想會發生什麼,

您的數組animal是在numberOfRowsInSection方法之前發佈的某處,然後它的內存由字符串獲取。

所以當你打電話給numberOfRowsInSection時,它會崩潰。所以你需要通過使用調試指針來進行手動檢查。

改變這一行本

animals = [[NSMutableArray arrayWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil] retain]; 

解決

animals = [NSMutableArray arrayWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil]; 

和dealloc中釋放出來。

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];

0

看起來你正在調用某個NSString對象。你運行調試器來查看你正在崩潰的確切代碼行嗎?看起來像你的代碼調用numberOfRowsInSection時。

0

檢查.h文件中變量動物的數據類型。我認爲它宣佈爲NSString。我這樣說是因爲如果你看看錯誤消息的第一行,它說

-[NSCFString count]: 

所以我覺得這會導致系統崩潰時,numberOfRowsInSection被稱爲它返回[animals count];

而且還如果u希望animals是一個數組,你應該像前面的答案中提到的那樣分配它。