Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

ios - Wrong indexpath with two custom UITableViewCell in table view

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...