开发者

didSelectRowAtIndexPath throws exception

This is probably some simple mistake, I just can't seem to find it in my code.

When ever I click a cell in my tableview I get an exception

This is my interface:

@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

I don't use XIB files so this is my loadView

- (void)loadView
{
    UIView *myview = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.title = @"Menu";
    UITableView *tableViewMenuItems = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, 150) style:UITableViewStyleGrouped];
    tableViewMenuItems.backgroundColor = [UIColor clearColor];
    tableViewMenuItems.delegate = self;
    tableViewMenuItems.dataSource = self;
    tableViewMenuItems.scrollEnabled = NO;
    [myview addSubview:tableViewMenuItems];
    [tableViewMenuItems release];
    self.view = myview;
    [myview release];
}

And this is the delegate method for selecting a row

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SendMessageViewController *sendMessageViewController = [[SendMessageViewController alloc] init];
    [self.navigationController pushViewController:sendMessageViewController animated:YES];
    [sendMessageViewController release];
}

And my bt

#0  0x94e3c9c6 in __pthread_kill ()
#1  0x98079f78 in pthread_kill ()
#2  0x9806abdd in abort ()
#3  0x9588a921 in abort_message ()
#4  0x958881bc in default_terminate ()
#5  0x010ee23b in _objc_terminate ()
#6  0x958881fe in safe_handler_caller ()
#7  0x95888268 in std::terminate ()
#8  0x958892a0 in __cxa_throw ()
#9  0x010ee416 in objc_exception_throw ()
#10 0x00f9c0bb in -[NSObject(NSObject) doesNotRecognizeSelector:] ()
#11 0x00f0b966 in ___forwarding___ ()
#12 0x00f0b522 in __forwarding_prep_0___ ()
#13 0x0008c870 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] ()
#14 0x00082b05 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:] ()
#15 0x0079c79e in __NSFireDelayedPerform ()
#16 0x00f7b8c3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#17 0x00f7ce74 in __CFRunLoopDoTimer ()
#18 0x00ed92c9 in __CFRunLoopRun ()
#19 0x00ed8840 in CFRunLoopRunSpecific ()
#20 0x00ed8761 in CFRunLoopRunInMode ()
#21 0x011d21c4 in GSEventRunModal ()
#22 0x011d2289 in GSEventRun ()
#23 0x00023c93 in UIApplicationMain ()
#24 0x00002589 in main (argc=1, argv=0xbffff698) at main.m:14
开发者_开发百科

What am I doing wrong?


Look at this line

-[NSObject(NSObject) doesNotRecognizeSelector:] ()

it clearly says that you are calling a method that doesn't exists somewhere in your code when clicking on the table view cell. Try searching for warnings in viewDidLoad and viewWill/DidAppear methods of SendMessageViewController


Ok found my problem:

In my delegate is was doing this

MenuViewController *menuViewController = [[[MenuViewController alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:menuViewController] autorelease];

So in the end I was calling methods on objects that where released. So I removed the autorelease and the problem was solved. Thanks for all that responded


Make sure you are calling the designated initialiser of UIViewController; since you aren't using a nib you should either call:

[[SendMessageViewController alloc] initWithNibName: nil bundle: nil];

Alternatively you could simply call this method in your view controller's init method.

If that doesn't solve it, set a symbolic breakpoint on NSException to get the code to stop at the point of problem and hopefully give you more info.

As an aside - any particular reason why you are creating a container view the size of the screen? You'll get one setup for free. You should just be able to add your UITableView as a sub view of the view controller's view, which would help simplify the code. Alternatively just use a UITableViewController.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜