简体   繁体   English

如何使用自定义UITableViewCell进行筛选? 将数据传递到详细视图控制器的问题

[英]How to Segue using Custom UITableViewCell? Problems with passing data to the detail view controller

I have created a custom UITableViewCell using the nib file. 我使用nib文件创建了一个自定义UITableViewCell。 Then when I select the my custom UITableViewCell, I want to perform a segue and pass the data to the detail view controller. 然后,当我选择我的自定义UITableViewCell时,我想执行segue并将数据传递到详细信息视图控制器。 I have already set the segue identifier in the storyboard, and I put the prepareForSegueWithIdentifier in the - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath However, when I perform the segue, only the first row of the table view can be shown on the detailed page. 我已经在情节提要中设置了segue标识符,并且将prepareForSegueWithIdentifier放在- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath但是,当我执行segue时,仅第一行表格视图可以显示在详细页面上。

I have three questions: 我有三个问题:

1. How to set the segue identifier for the custom UITableViewCell? 1.如何设置自定义UITableViewCell的序列标识符?

Since I create the custom UITableViewCell in the xib file, it is difficult to connect the xib file with the main storyboards with other view controllers. 由于我在xib文件中创建了自定义UITableViewCell,因此很难将xib文件与主要故事板与其他视图控制器连接起来。

2. If I change the class name of the table view cell in the storyboard, will it be my custom UITableViewCell? 2.如果我在情节提要中更改表视图单元的类名,它将是我的自定义UITableViewCell吗?

Specifically, in order to replace the default UItableViewCell in the storyboard to my custom UITableViewCell, I change the name of the class of the default UITableViewCell in the storyboard, to the name of my custom UITableViewCell. 具体来说,为了将情节提要中的默认UItableViewCell替换为我的自定义UITableViewCell,我将情节提要中的默认UITableViewCell的类的名称更改为我的自定义UITableViewCell的名称。 Then I Control + Drag to create a push segue and set the segue identifier. 然后,我按Control +拖动以创建推送Segue并设置Segue标识符。 But it seems this one doesn't work either. 但似乎这也不起作用。

3. Why every time I perform segue, the destination view controller only show the specific content of the first row of my table view? 3.为什么每次执行segue时,目标视图控制器仅显示表视图第一行的特定内容?

That is, whenever I select the first cell, the second cell, the third cell......, all I get after segue to the destination view controller is the content of the first cell. 也就是说,无论何时选择第一个单元格,第二个单元格,第三个单元格……,选择到目标视图控制器后,我得到的都是第一个单元格的内容。 I am wondering whether the sender is my custom UITableViewCell? 我想知道发件人是否是我的自定义UITableViewCell? Or is the indexPath is nil? 还是indexPath为nil? I am not sure. 我不确定。

My code is as follows: 我的代码如下:

- (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;
            }
        }

}

Did you try debugging the method and looking at the value of sender? 您是否尝试调试该方法并查看发送方的价值? You provided the indexPath in the call to performSegueWithIdentifier:sender: but the you use the sender as a cell in indexPathForCell: . 您在对performSegueWithIdentifier:sender:的调用中提供了indexPath ,但是您将发件人用作indexPathForCell:的单元格。


I already see problems in your tableView:cellForRowAtIndexPath: method as well. 我已经在您的tableView:cellForRowAtIndexPath:方法中看到了问题。 While scrolling the cells get reused, so when you perform a async call the block can and will be executed on the wrong cell. 滚动时,单元将被重用,因此,当您执行异步调用时,该块可以并且将在错误的单元上执行。 To prevent that call the dequeueReusableCellWithIdentifier: again inside the dispatch_async callback. 为了防止这种情况,请在dispatch_async回调中再次调用dequeueReusableCellWithIdentifier:
Also you are adding a subview directly to the cell instead of using the contentView property. 另外,您将直接向该单元格添加一个子视图,而不是使用contentView属性。

Regarding your questions, as far as I know you cannot connect a segue from a nib file. 关于您的问题,据我所知您无法连接笔尖文件中的segue。 To make it work properly you need to connect the segue from the prototype cell in the storyboard. 为了使其正常工作,您需要从情节提要中的原型单元连接segue。 You created a manual segue and you're calling it when the user touches the cell. 您创建了一个手动序列,并在用户触摸单元格时调用它。 I think that it'll be easier for you to understand what's happening if you don't use segues at all and just call showViewController:sender: yourself. 我认为,如果您根本不使用segues,而只是自己调用showViewController:sender: ,这showViewController:sender:您更容易理解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不使用Segue的情况下将数据从一个视图控制器传递到另一个 - Passing data from one view controller to another without using Segue 在详细视图控制器中查看错误 - Segue Error in detail view controller 将数据传递给实例化的视图控制器而无需转场 - Passing data to instantiated view controller without segue 在segue上将数据传递到视图控制器(Swift) - Passing data on segue to view controller (Swift) 使用Segue将数据传递到其他View Controller的问题 - Issue Passing Data To Different View Controller With Segue 使用Segue从先前的View Controller传递到另一个View Controller时,数据变为Nil - Data turns Nil when passing from previous View Controller going to another View Controller using Segue 使用“显示详细信息” segue替换溢出的视图控制器中的详细视图控制器 - Using “Show detail” segue to replace detail view controller in spilt view controller 从自定义UITableViewCell到包含数据的弹出视图的选择方式 - Segue way from custom UITableViewCell to a popup View with data 在表视图控制器到详细视图控制器之间传递数据时出错 - Error passing data between table view controller to detail view controller 将 Firebase 数据从一个视图控制器传递到详细视图控制器 - Passing Firebase Data from one view controller to detail view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM