0
votes

I have created a custom UITableViewCell using the nib file. Then when I select the my custom UITableViewCell, I want to perform a segue and pass the data to the detail view controller. 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.

I have three questions:

1. How to set the segue identifier for the custom 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.

2. If I change the class name of the table view cell in the storyboard, will it be my custom 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. Then I Control + Drag to create a push segue and set the segue identifier. 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?

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? Or is the indexPath is 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;
            }
        }

}
1

1 Answers

0
votes

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:.


I already see problems in your tableView:cellForRowAtIndexPath: method as well. 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.
Also you are adding a subview directly to the cell instead of using the contentView property.

Regarding your questions, as far as I know you cannot connect a segue from a nib file. To make it work properly you need to connect the segue from the prototype cell in the storyboard. 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.