Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Tuesday, October 1, 2013

Handling Row Selection in UITableView

So, you want to handle table row selection in a UITableView, eh?

Oftentimes you want to do something interesting when a row is selected in your table view. Such interesting things include navigating to a detail screen or displaying a popup of some kind. Today we're going to show you an example of the former. But, hold on tight to your hat, because we're going to make some bold assumptions about the way you've architected your view. 

Our assumption is that you have a UIViewController set as the class for your view and you have already registered your UIViewController class as the delegate for the table. We're also going to assume you have a NSArray property that contains a Model messages. Further, our example is going to show you what it would look like to navigate to a detail message screen, as if you were creating a mail app.

Lets see some code, shall we?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    UIStoryboard *storyboard = self.storyboard;

    Message *message = [self.messageCollection objectAtIndexPath:indexPath];

    MessageViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"MessageViewController"];
    [viewController setMessage:message];
    [self.navigationController pushViewController:viewController animated:YES];

In the above example, we're handling row selection, getting the object that belongs to the row and segueing to another view controller. If that seems like a lot of concepts to bite off, you're probably right. I'll expand on this more in a full UITableViewController example soon.

Well for now, there you have a snippet of code to implement a useful bit of logic. Best of luck in your coding ventures.

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates