关于Objective C:检索要传递给模态编辑视图的UITableViewCell标签值

Retrieving UITableViewCell Label Values For Passing To Modal Edit View

我正在尝试将托管对象的现有属性值传递到模式segue视图以进行编辑。从"编辑模式"开始,我将从UITableViewController子类(BreadRackTableViewController)开始,使用模式搜索通过嵌入式UINavigationController转换为UIViewController子类(EditBreadRackTableViewController)。正常的TableViewCell选择将推送到另一个UITableViewController子类,但这暂时并不重要。

我已将属性添加到编辑视图控制器标题(和@synthesized)中,以用作吸气剂:

1
2
@property (nonatomic, strong) id existingBreadRackDataName;
@property (nonatomic, strong) id existingBreadRackDataDetails;

我要做的是使用现有的核心数据实体属性填充编辑视图的字段,这些属性先前已由用户设置(理论上)。如果传递文字字符串值,则可以显示数据,但是我需要检索并传递现有数据。

这是我目前拥有的:

1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"editBreadRackSegue"])
    {
        EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
        // [ebrtvc setBreadRackClass:breadRackClass];
        ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
        ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:@"breadRackDataDetails"];
    }
}

这最??后两行代码...

1
2
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:@"breadRackDataDetails"];

...它们都返回nil。但是,如果我指定文字字符串,则该文字字符串会显示在需要的位置。

所有这些,我的问题是:

How do I retrieve the attribute's key value for that selected row (or just the textLabel and detailTextLabels), and assign it to my setter?

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"editBreadRackSegue"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
        [segue.destinationViewController setExistingBreadRackDataName:[breadRackClass valueForKey:@"breadRackDataName"]];
        [segue.destinationViewController setExistingBreadRackDataDetails:[breadRackClass valueForKey:@"breadRackDataDetails"]];
    }

    // code for other segues here...
}

我最终删除了ebrtv变量,并清理了一点代码。但是有效的是,在为我的BreadRackClass声明indexPath时使用indexPathForCell:sender而不是indexPathForSelectedRow。


我认为indexPathForSelectedRow可能是个问题。

您可以将indexPath从didSelectRowAtIndexPath传递给prepareForSegue,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"editBreadRackSegue" sender:indexPath];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"editBreadRackSegue"])
    {
        NSIndexPath *clickedIndexPath = (NSIndexPath*)sender;
        if( [clickedIndexPath isKindOfClass:[NSIndexPath class]] ){

            BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:clickedIndexPath];

            EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];
            ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
            ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:@"breadRackDataDetails"];
        }
    }
}

让我知道它是否对您有用。