表视图中有两个自定义 UITableViewCell 的错误索引路径 [英] Wrong indexpath with two custom UITableViewCell in table view

查看:22
本文介绍了表视图中有两个自定义 UITableViewCell 的错误索引路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格视图中有两个可以正确显示的自定义单元格.MyCustomCellB 有一个按钮,当用户点击这个按钮时,我以编程方式调用 segue,这也有效,但导入了错误的变量.它采用属于"MyCustomCellA 类型单元格的不同数据.但是,如果我首先选择单元格然后点击按钮它工作正常,在选择之后它会以某种方式获得正确的数据.

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.

我的 cellForRowAtIndexPath: 不正确吗?

- (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"];

        }
    }

推荐答案

首先,由于你在你的按钮方法中除了调用 performSegue 之外没有做任何事情,你应该直接从按钮而不是控制器进行转场,并删除按钮的操作方法.然后在 prepareForSegue:sender: 中,sender 参数将是按钮.您需要获取包含按钮的单元格的 indexPath.有多种方法可以做到这一点.一种方法是使用表格视图方法 indexPathForRowAtPoint:,并将按钮的原点(转换为表格视图的坐标系后)作为点传递.

First of all, since you don't do anything in your button method other than call performSegue, you should make the segue directly from the button instead of the controller, and delete the button's action method. Then in prepareForSegue:sender:, the sender argument will be the button. You need to get the indexPath of the cell that the button was contained in. There are multiple ways to do that. One way is to use the table view method indexPathForRowAtPoint:, and pass the button's origin (after converting to the table view's coordinate system) as the point.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {
    if ([[segue identifier] isEqualToString:@"info"]) {
        if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {
            CGPoint p = [sender convertPoint:CGPointZero toView:self.tableView];
            NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];

            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"];

        }
    }

我不知道您是否需要对desc"segue 进行相同的更改,因为我不知道该更改是如何触发的.

I don't know whether you need to make the same change for the "desc" segue since I don't know how that one is being triggered.

这篇关于表视图中有两个自定义 UITableViewCell 的错误索引路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆