简体   繁体   中英

Tap on table row push a new view

I have a table . When I click on a table row I want to push a new view . I know that there in navigationController and how to push it , but I want to use such code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller

    TasksViewController *tasks = [[TasksViewController  alloc] 
                    initWithNibName:@"Tasks" 
                    bundle:nil];

    LoginAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [delegate.window addSubview:[tasks view]];
    [self.view removeFromSuperview];        
}  

The TaskViewCOntroller is a view I want to push like this. When I try to execute this i have an exception like this:

*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678
2011-09-16 12:26:41.118 LeoAction[1149:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00df55a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f49313 objc_exception_throw + 44
    2   CoreFoundation                      0x00dadef8 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x000e03bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x0035ac91 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 883
    5   UIKit                               0x003504cc -[UITableView(UITableView

I do not know what to do with this exception?

The exception is telling you that you have a problem in the tableView:cellForRowAtIndexPath: method. It sounds like you are not providing a proper return path for every instance of the method being called.

Check that at the end of this method you have something like return cell; .

As for your navigation, you should do something like:

[self.navigationController pushViewController:tasks animated:YES];

UIViewController defines the navigation controller for you, so pushing a new one is as easy as instantiating it and calling the above method.

EDIT

With regards to the navigation controller - when you setup your top level table view controller, you need it to be owned by a UINavigationController. I'll assume that you're working with Interface Builder for simplicity.

In your MainWindow.xib , drag in a Navigation Controller object. Expand its disclosure arrow, and you'll see that it contains a navigation bar, and a root view controller. Select this root view controller and in the identity inspector, change its class to your custom table view controller, and in the attributes inspector change its NIB Name field to the appropriate nib. Hook up your navigation controller to the App Delegate object as required.

You should then find that you can use the pushViewController:animated: method for self.navigationController from within tableView:didSelectRowAtIndexPath: .

I hope this helps.

您应该像这样推动它:

[self.navigationController pushViewController:taskViewController animated:YES];

U can Push Like Bellow

TasksViewController *tasks = [[TasksViewController  alloc] 
                initWithNibName:@"Tasks" 
                bundle:nil];

[self.navigationController pushViewController:tasks animated:YES];
[tasks release];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM