user1146842
user1146842

Reputation:

Sharing data between iPhone tableviews?

I want to be able to share data between table views for an app that I'm making. The reason for this is that I want to be able to tell a subview which table row was selected so that I don't have to make a bunch of views and I can just test to see what the integer variable was. I watched a video tutorial on how to do this but they did not use tableviews. So when I tried this it did not seem to work. I used the app delegate as a "data center" that held that variables and then I tried to assign values to the variables in didSelectRowAtIndexPath method. (Pushing the new view works fine by the way it's just the shared application) Here's the code for the first tableview where I assign the variable to a number.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    ApplicationAppDelegate *appDelegate = (ApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.rowPicked = row;
    SecondLevelViewController *nextController = [self.controllers objectAtIndex:row];
    [self.navigationController pushViewController:nextController animated:YES];
}

In the app delegate I did this and I synthesized it in the .m file:

   @property (nonatomic) NSInteger rowPicked;

As well as other NSIntegers that I needed. Any suggestions? If you think I'm doing this totally wrong could you please enlighten me with specific instructions or a link to a website or video tutorial? Thank you all!

Upvotes: 1

Views: 142

Answers (2)

OdNairy
OdNairy

Reputation: 540

It's really looks bad. Much better add id<$YOUR_PROTOCOL> delegate to SecondLevelViewcontroller and set nextController.delegate = self. Protocol can looks like

@protocol RowAccessProtocol
@optional
-(NSUInteger)selectedRow;
@end

Your current tableViewController must be created:

@protocol RowAccessProtocol;
@class FirstLevelTableViewController:UITableViewController<RowAccessProtocol>
…
@end

And implementation:

…
-(NSUInteger)selectedRow{
return [self.tableView indexPathForSelectedRow].row;
}

In SecondLevelViewController you can call [self.delegate selectedRow] to get selected row.

Upvotes: 0

Jesse Bunch
Jesse Bunch

Reputation: 6679

Here's how I usually accomplish this:

  • I don't use the AppDelegate for this. The logic and model data for both list and detail should reside in classes that make sense. In this case, we'll use the list and detail view controller classes themselves.
  • I'll begin by creating a UITableViewCell sublcass for the list view's row. In that sublcass, I'll create an ivar that houses the "entity" or whatever data the cell will need to display it's information. This can be an NSManagedObject or even an NSDictionary.
  • I'll override the setter of that ivar so that when data is set on the UITableViewCell, it updates the cell outlets to display it correctly. Notice how I keep the logic of how the cell is displayed contained completely within the subclass. It's important that you do things like this throughout your application to promote code cleanliness and organization.
  • In your tableView:didSelectRowAtIndexPath method, you'd then call the UITableView class' cellForRowAtIndexPath: method to return the cell that was selected. You can then cast it to your UITableViewCell subclass and get the entity information you set earlier.
  • Next, you'll need to create an ivar in your detail view controller. You'll want to set this variable from the tableView:didSelectRowAtIndexPath: method---right before you push the detail view onto the stack.
  • You should now have the necessary data in your detail view controller sublcass for processing, querying, or whatever.

Hope this helped!

Upvotes: 1

Related Questions