I'm trying to figure out the best way for me to use the text from a UITableViewCell to set/change a UILabel in it's parent view. And I'd like it to be done when the user taps on a row. For more clarity, here is a simplified physical representation:

From this:

http://i.stack.imgur.com/O9ipE.png

To this, when the user taps on the row:

http://i.stack.imgur.com/fmeIb.png

I was thinking I probably just need to use delegates & protocols but then how would I actually change the UILabel? Wouldn't that just pass the text back, instead of passing it back and changing the UILabel? Also, I don't know very much about unwind segues, but could I use one of those to do this instead?

有帮助吗?

解决方案

Yes, you would use a delegate method to pass the data back to the parent. Since you are using some kind of array datasource to populate the table, you already have access to those values in a clean format (instead of pulling from the tableViewCell's hierarchy).

From your table vc:

@protocol MyTableViewDelegate
-(void)didSelectText:(NSString *)text;
@end

then implement the -didSelectText: in your parent VC.

When the user selects the cell, you will have the indexPath, which gives you a section and row.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // It's good practice to do this check first
    if([self.delegate respondsToSelector:@selector(didSelectText:)]){
          //basically pull the value out of your data source and send it to the delegate
        [self.delegate didSelectText: self.arrayDataSource[indexPath.section][indexPath.row]];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top