开发者

Help to have check box inside tableview

When i select the row , i get the crash like below ..pls help me out

-[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 GSEvent开发者_如何学GoRun + 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


I think what happen,

Your array animal is somewhere released before numberOfRowsInSection method and then its memory is taken by a string .

so when you call count in numberOfRowsInSection then it get crash. so you need to manually check by using debugging pointer where it is happening.

change this line for solving

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

by this

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

and release it in dealloc.


You haven't allocated the animals array.

replace the following line

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

With

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

Then release the object in the dealloc method: means add [animals release]; in dealloc method.


It looks like you are calling count on an NSString object somewhere. Have you run the debugger to see the exact line of code where you are crashing? Looks like it is when your code calls numberOfRowsInSection.


Check the data type of the variable animals in the .h file. I think its declared as NSString. I say this because if you look at the first line of the error message it says

-[NSCFString count]:

So I think this causes a crash when the numberOfRowsInSection is called where it returns [animals count];

And also if u want the animals to be an array you should allocate it as mentioned in the earlier answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜