我正在使用storyBoard开发Iphone应用程序。
我在UINavidationView中有一个UITableView。我在自定义单元格中加载数据。然后当用户点击一个单元格时,我转到另一个视图(ResultView)。
我在故事板中设置了视图和segue。
我的目标是从prepareForSegue方法将数据传递给ResultView。
为此,我创建了一个实现UITableViewCell的自定义单元格。然后我添加了一个名为NSDate的属性:creationDate。我需要将所选单元格的创建日期传递给ResultView。 我有以下内容 ate = readingCell.creationDate;
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"resultViewSegue"])
{
//Get a reference to the destination
ResultsViewController * destinationView = segue.destinationViewController;
//I try to get the selected cell in order to pass it's property
historyCellClass * selectedCell = (historyCellClass*) sender;
//pass the creation date to the destination view (it has its own creation date property)
[destinationView setCreationDate:selectedCell.creationDate];
}
}
但是,结果视图的创建日期始终为null。
看起来我没有获得所选单元格的引用以便读取其属性。
如何将单元格的日期传递给下一个视图?
非常感谢您的帮助
答案 0 :(得分:1)
我处理这个问题的方法是手动触发segue和表示选择状态的ivar。
确保触发的segue从一个视图控制器转到另一个视图控制器(而不是从一个tableView单元格)。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedModel = [self.myModel objectAtIndex:indexPath:row];
[self performSegueWithIdentifier:@"resultsViewSegue"];
selectedModel是新的ivar,其类型与支持表数据源的数组中的单个元素相同。完全按照在cellForRowAtIndexPath中的方式查找索引路径:。
现在在prepareForSegue:..
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"resultViewSegue"])
{
//Get a reference to the destination
ResultsViewController * destinationView = segue.destinationViewController;
//pass the creation date to the destination view (it has its own creation date property)
[destinationView setCreationDate:self.selectedModel.creationDate];
// selectedModel.creation date might not be right... use whatever way you get to creationDate
// from the indexPath in cellForRowAtIndex path, that's the code you want above.
}
}
在表视图选择和segue开头之间保存的状态有几种选择。您可以按照我的建议保存选定的索引路径或模型元素,或者只保存要传递的模型的方面(例如,您的creationDate)。保存状态的唯一不好的想法是表格单元本身。