如何在另一个视图中调用indexPath?

时间:2014-08-08 11:30:41

标签: ios nsindexpath

我有两个视图listOptionTable.m(UITableViewController)和listOptionCell.m(UITableViewCell)。

我试图在deselectRowAtIndexPath中调用listOptionCell.m方法,而我需要获取indexPath以便它可以运行。

请问如何从indexPath获取listOptionTable.m

listOptionCell.h

  listOptionTable *tableVC;

listOptionCell.m

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:NO];

    if (isSelected){

        [tableVC.tbl deselectRowAtIndexPath:`indexPath` animated:YES]; //I have to call indexPath in here.

    }
    else
    {
     ....
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你的细胞行为很奇怪。无论如何你可以做到这一点:

  1. 将属性添加到自定义单元格(listOptionCell.m):

    @property (strong, nonatomic) NSIndexPath *indexPath;
    
  2. 在listOptionTable.m中实现UITableViewDataSource方法,如:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      listOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
      // Configure the cell...
      cell.indexPath = indexPath;
      return cell;
    }
    
  3. 现在您可以在单元格中获取indexPath:

    [tableVC.tbl deselectRowAtIndexPath:self.indexPath animated:YES];
    
  4. 更好的方法是在单元格中声明自定义协议,然后让委托在索引路径中取消选择行。