Ios 如何使用自定义UITableViewCell进行分段?向局部视图控制器传递数据时出现问题

Ios 如何使用自定义UITableViewCell进行分段?向局部视图控制器传递数据时出现问题,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我已经使用nib文件创建了一个自定义UITableViewCell。然后,当我选择my custom UITableViewCell时,我想执行一个序列,并将数据传递给详图视图控制器。我已经在故事板中设置了segue标识符,并且我将prepareForegueWithIdentifier放在-void tableView:UITableView*tableView didSelectRowAtIndexPath:NSIndexPath*indexPath中。但是,当我执行segue时,在详细页面

我已经使用nib文件创建了一个自定义UITableViewCell。然后,当我选择my custom UITableViewCell时,我想执行一个序列,并将数据传递给详图视图控制器。我已经在故事板中设置了segue标识符,并且我将prepareForegueWithIdentifier放在-void tableView:UITableView*tableView didSelectRowAtIndexPath:NSIndexPath*indexPath中。但是,当我执行segue时,在详细页面上只能显示表视图的第一行

我有三个问题:

一,。如何为自定义UITableViewCell设置segue标识符

由于我在xib文件中创建了自定义UITableViewCell,因此很难将xib文件与其他视图控制器的主情节提要连接起来

二,。如果我在故事板中更改表视图单元格的类名,它会是我的自定义UITableViewCell吗

具体来说,为了将情节提要中的默认UItableViewCell替换为自定义UItableViewCell,我将情节提要中默认UItableViewCell的类的名称更改为自定义UItableViewCell的名称。然后我控制+拖动以创建推送序列并设置序列标识符。但是这个似乎也不起作用

三,。为什么每次执行segue时,目标视图控制器只显示表视图第一行的特定内容

也就是说,每当我选择第一个单元格、第二个单元格、第三个单元格……,在切换到目标视图控制器后,我得到的只是第一个单元格的内容。我想知道发件人是否是我的自定义UITableViewCell?还是索引为零?我不确定

我的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    customTableViewCell *cell;
    cell = (customTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (!cell) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"customTableViewCell" owner:nil options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSDictionary *photo = self.recentPhotos [indexPath.row];


    dispatch_queue_t queue = dispatch_queue_create("fetch photos", 0);
    dispatch_async(queue, ^{

        NSURL *imageURL = [FlickrFetcher URLforPhoto:photo format:FlickrPhotoFormatSquare];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

        dispatch_async(dispatch_get_main_queue(), ^{

            NSString *title = [photo valueForKeyPath:FLICKR_PHOTO_TITLE];

            cell.cellLabel.text = title;



            UIImageView *cellImageView = [[UIImageView alloc]initWithImage:image];
            [cellImageView setFrame: CGRectMake(10, 5, 45, 45)];
            [cellImageView.layer setCornerRadius:cellImageView.frame.size.width/2];
            cellImageView.clipsToBounds = YES;

            [cell addSubview:cellImageView];

        });
    });

      return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"go" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        NSDictionary *photo = self.recentPhotos [indexPath.row];
        NSURL *imageURL = [FlickrFetcher URLforPhoto:photo format:FlickrPhotoFormatLarge];
        NSString *title = [photo valueForKeyPath:FLICKR_PHOTO_TITLE];

        if ([segue.identifier isEqualToString:@"go"]) {
            if ([segue.destinationViewController isKindOfClass:[ViewImageViewController class]]) {
                ViewImageViewController *vivc = (ViewImageViewController *) segue.destinationViewController;
                vivc.imageURL = imageURL;
                vivc.title = title;
            }
        }

}

您是否尝试调试该方法并查看sender的值?您在调用performSegueWithIdentifier:sender:时提供了indexPath,但您将sender用作indexPathForCell:中的单元格

我已经在tableView:cellforrowatinexpath:method中看到了问题。滚动时,单元格会被重用,因此,当您执行异步调用时,该块可以并且将在错误的单元格上执行。为了防止这种调用,dequeueReusableCellWithIdentifier:再次位于dispatch\u异步回调中。 此外,您将直接向单元格中添加子视图,而不是使用contentView属性。
关于您的问题,据我所知,您无法从nib文件连接segue。要使其正常工作,您需要从故事板中的原型单元连接segue。您创建了一个手动segue,当用户触摸手机时,您将调用它。我认为,如果您根本不使用segues,只需拨打showViewController:sender:您自己的电话,您就会更容易了解发生了什么。

非常感谢!我尝试调试发送方的值。每当我选择单元格时,控制台将显示如下2014-10-24 16:32:53.161 Flickr照片应用程序[1484:70177]{length=2,path=0-3}2014-10-24 16:32:56.744 Flickr照片应用程序[1484:70177]{length=2,path=0-1}。此外,我尝试调试indexPath的值,它总是0。发送方是indexPath。你是在打电话给segue时发送的。在其上打印row属性,您应该会看到正确的值