简体   繁体   English

iOS:UITableView和“后退”按钮,如何在父视图中更新单元格?

[英]iOS:UITableView & Back Button, how to update cell in parent view?

I searched, but didn't find good answer to this: 我进行了搜索,但找不到很好的答案:

It should be pretty straightforward, I navigate from a parent TableView to a child TableView that implements only the selection option for a cell in parent View. 它应该非常简单,我从父TableView导航到子TableView,该子TableView仅实现父View中单元格的选择选项。

In childTableView, I would like to pass back the value chosen by user in child view (selection one of the cell in this view) to parent view and update the "cell.detailTextLabel.text" in parent view, I can find when the back button in child view got tapped but cannot find how/where to update the cell in parent view with new value got from child view's cell. 在childTableView中,我想将用户在子视图(此视图中的单元格之一)中选择的值传递回父视图,并更新父视图中的“ cell.detailTextLabel.text”,我可以找到何时返回轻击了子视图中的按钮,但无法找到如何/在何处/从子视图的单元格获取新值来更新父视图中的单元格。

My second question is how to pass the data to parent view. 我的第二个问题是如何将数据传递到父视图。

Try the following 尝试以下

In child view, define a protocol 在子视图中,定义协议

// childViewController.h
@protocol ChildDelegate;

@interface childViewController
{
    id <ChildDelegate> delegate;
}

@property (nonatomic, weak) id <ChildDelegate> delegate;
@end // interface

@protocol ChildDelegate <NSObject>
    - (void) selectedData: (NSString*) text;
@end

Send data when user selects a cell 用户选择单元格时发送数据

// childViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    ...
    // send info the mainViewController
    [self.delegate selectedData: (NSString*) [dataSource objectAtIndex: indexPath.row]];
}

In mainViewController, implement this protocol 在mainViewController中,实现此协议

- (void) selectedData: (NSString*) text
{
   // retrieve current selected row, and update with text.
}

// don't forget to set the delegate when you create the childViewController //创建childViewController时不要忘记设置委托

child = // create child
child.delegate = self;
// save currently selected row
// push childViewController

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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