English 中文(简体)
表格之间共享数据?
原标题: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];
}

在指定代表中,我这样做,我将其综合整理成:

   @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!

问题回答

这里我通常如何做到这一点:

  • 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.

希望帮助!

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

页: 1 必须要有:

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

执行:

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

在第二位书记员中,请打电话<代码>[自选Row],以获得选定行文。





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签