我无法使某些事情发挥作用:
我有一个UIViewController,其中包含一个已连接的tableView并设置了委托。我试图从didSelectRowatIndexPath发送一个segue并在prepareforSegue中我试图读取一些选定的单元格数据(我使用模型来检索实际的重要数据,但我有一些元素,即我想要的imageView从单元格读取)
让我感到困惑的是,无论我如何设置发件人(self.tableView或self或indexPath或任何东西),在prepareforSegue中我总是将单元格设为nil。
如果我将UIViewController更改为UITableViewController并将发送者作为tableView,那么一切都运行得很好。
我错过了什么?
代码遵循:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"bundleItemDetails" sender:tableView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"bundleItemDetails"])
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
itemCell *cell = (itemCell*)[self.tableView cellForRowAtIndexPath:selectedIndexPath];
NSDictionary *currentProduct = [[[productsArray objectAtIndex:selectedIndexPath.section] objectForKey:@"products"] objectAtIndex:selectedIndexPath.row];
UINavigationController *navController = [segue destinationViewController];
itemDetails *bundleProducts = (itemDetails*)([navController viewControllers][0]);
UIImage *img = cell.itemImage.image;
bundleProducts.valueImage = img;
bundleProducts.valueName = [currentProduct valueForKey:@"title"];
bundleProducts.valueDescription = [currentProduct valueForKey:@"description"];
bundleProducts.detailFullPrice.text = [currentProduct valueForKey:@"price"];
}
}
这与UITableViewController完美配合,并为UIViewController的单元格值返回nil
更新:一些新发现
看起来indexPathForSelectedRow返回null和0.如果我将didSelectRowAtIndexPath的代码更改为:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:YES];
[self performSegueWithIdentifier:@"bundleItemDetails" sender:self.tableView];
}
我得到了索引值,但我仍然有空的itemCell(itemCell是一个带有外部.xib的自定义单元格,是否需要在UIViewController中以不同于UITableviewController的方式调用它?
答案 0 :(得分:0)
检查您的tableView tableView.allowsSelection
是否为YES
答案 1 :(得分:0)
您可以尝试使用属性来保存您想要在segue中传递的值。您可以在didSelectRowAtIndexPath:
中设置属性,然后在prepareForSegue:sender:
中访问它们。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.valueImage = cell.itemImage.image;
// etc...
[self performSegueWithIdentifier:@"bundleItemDetails" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"bundleItemDetails"]) {
UINavigationController *navController = [segue destinationViewController];
itemDetails *bundleProducts = (itemDetails*)([navController viewControllers][0]);
bundleProducts.valueImage = self.valueImage;
bundleProducts.valueName = self.valueName;
bundleProducts.valueDescription = self.valueDescription;
bundleProducts.detailFullPrice.text = self.detailFullPrice;
}
}