Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two custom cells in my table view that I can display correctly. MyCustomCellB has a button and when the user taps this button I call a segue programatically, that also works, but imports wrong variables. It takes different data which "belongs" to a MyCustomCellA typed cell. But, if I first select the cell and then tap the button it works fine, somehow after the selection it gets the right data.

Is my cellForRowAtIndexPath: is incorrect?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([object[@"num"] isEqualToString:@"one"]) {

        MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

         //... cell display
        cell.acceptLabel.tag = indexPath.row;
        [cell.acceptLabel addTarget:self action:@selector(didTapBttn:) forControlEvents:UIControlEventTouchUpInside];
        return cell;

    } else {

        MyCustomTableViewCellB *cellMessage = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];

        //... cell display

        return cellMessage;

    }

}

- (void)didTapBttn:(id)sender {

    [self performSegueWithIdentifier:@"info" sender:self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"info"]) {
        if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

            DViewController *datap = [segue destinationViewController];

            PFObject *passObj = [self.objects objectAtIndex:indexPath.row];

            datap.infoName = passObj[@"infoName"];
            datap.infoId = passObj[@"id"];

        }

    if ([[segue identifier] isEqualToString:@"desc"]) {
        if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

            DViewController *datap = [segue destinationViewController];

            PFObject *passObj = [self.objects objectAtIndex:indexPath.row];

            datap.infoN = passObj[@"descName"];
            datap.infoId = passObj[@"descId"];

        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
146 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can extend your didTappBttn with some data (i.e. NSDictionary) so you have to be sure it always contains desired data.Also tag in your code is not relevant if you don't interpret it. Maybe data should be row at indexpath tag.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...