如何从Cell获取UITableViewCell indexPath?

sin*_*inθ 70 iphone xcode cocoa objective-c ios

我如何从一个细胞,得到了indexPathUITableView

我搜索了堆栈溢出和谷歌,但所有的信息是相反的.有没有办法访问superView/ UITableView然后搜索单元格?

关于上下文的更多信息:我有两个类,一个被调用Cue,一个被调用CueTableCell(它的子类UITableViewCell)CueTableCell是可视化表示Cue(两个类都有指向彼此的指针).Cue对象在链表中,当用户执行某个命令时,需要选择CueTableCell下一个的可视表示()Cue.因此Cue类调用select的下一个方法Cue列表,以检索UITableViewcell并调用它selectRowAtIndexPath:animated:scrollPosition:,为它需要indexPathUITableViewCell.

Mun*_*ndi 133

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Run Code Online (Sandbox Code Playgroud)

它有助于阅读文档,即使这被认为是有争议的(见下面的评论).

该单元没有业务知道它的索引路径是什么.该控制器应该包含任何能够根据数据模型的UI元素的任何代码或修改基于UI交互的数据.(见MVC.)

  • 如果你阅读这个问题会很有帮助.这是来自UITableViewCell,它没有tableView属性(并且所有帐户都不应该). (40认同)
  • @voidref它也有助于思考这个问题;-).该单元没有业务知道它的索引路径是什么.所以这段代码应该在控制器中.有关MVC的背景信息,请参阅[Cocoa Core Competencies](https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html). (13认同)
  • 不回答原来的问题.有时候细胞需要知道它的路径.例如,如果您在子类化单元格上实现了某种形式的滑动操作,则可能需要通知该父视图当前刷过哪个单元格.一种方法是让parentView中的方法循环遍历tableView单元格并检查已在单元格中设置的iVar.更好的方法是让单元格通过委托方法通知parentView它的indexPath ...因此单元格需要知道它的indexPath.所以Mundi,更好地回答这个问题然后说出你的观点. (9认同)
  • 索引路径是单元格和数据模型之间的"粘合剂",因此它属于控制器.上面的长篇评论实际上是错误的.@ wuf810 ...因为控制器知道单元格和索引路径之间的关系,所以单元格不会(也不应该). (4认同)
  • 如果从一开始人们需要正确的答案就很明显并不重要. (2认同)

Pep*_*per 25

从你的UITableViewCell尝试:

NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];
Run Code Online (Sandbox Code Playgroud)

编辑:似乎这不适用于iOS 8.1.2及更高版本.感谢Chris Prince的指点.

  • 在iOS 8.1.2中,表视图单元格的超级视图属于UITableViewWrapperView类型.它的超级视图是表视图类型.但我认为我们在这里踩着危险的地方...... (7认同)
  • 一切都可以追溯到“该单元不知道其索引路径是什么”。论点。访问Superview几乎总是一个hack,而且一段时间后不起作用也就不足为奇了。 (2认同)

AIT*_*ANE 11

您可以尝试这个简单的提示:

在你的UITableViewCell班级:

@property NSInteger myCellIndex; //or add directly your indexPath 
Run Code Online (Sandbox Code Playgroud)

并根据您的cellForRowAtIndexPath方法:

...
[cell setMyCellIndex : indexPath.row]
Run Code Online (Sandbox Code Playgroud)

现在,您可以在任何地方检索您的单元索引

  • 您需要记住在重新排序或删除单元格后手动更新此信息. (3认同)

mik*_*eho 10

为了解决那些说"这是一个坏主意"的人,在我的情况下,我需要的是我有一个按钮UITableViewCell,当按下时,它是另一个视图.由于这不是细胞本身的选择,[self.tableView indexPathForSelectedRow]不起作用.

这给我留下了两个选择:

  1. 将我需要传递的对象存储到表格单元格本身的视图中.虽然这工作,它会破坏我的点有一个NSFetchedResultsController,因为我不做要存储在内存中的所有对象,特别是如果该表是长.
  2. 使用索引路径从提取控制器检索项目.是的,看起来很丑,我必须NSIndexPath通过黑客来弄清楚,但它最终比在内存中存储对象便宜.

indexPathForCell:是正确的使用方法,但这是我将如何做到这一点(假设这个代码是在以下子类中实现的UITableViewCell:

// uses the indexPathForCell to return the indexPath for itself
- (NSIndexPath *)getIndexPath {
    return [[self getTableView] indexPathForCell:self];
}

// retrieve the table view from self   
- (UITableView *)getTableView {
    // get the superview of this class, note the camel-case V to differentiate
    // from the class' superview property.
    UIView *superView = self.superview;

    /*
      check to see that *superView != nil* (if it is then we've walked up the
      entire chain of views without finding a UITableView object) and whether
      the superView is a UITableView.
    */
    while (superView && ![superView isKindOfClass:[UITableView class]]) {
        superView = superView.superview;
    }

    // if superView != nil, then it means we found the UITableView that contains
    // the cell.
    if (superView) {
        // cast the object and return
        return (UITableView *)superView;
    }

    // we did not find any UITableView
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

PS我的真实代码可以从表格视图中访问所有这些,但我举一个例子,说明为什么有人可能想直接在表格单元格中执行此类操作.


Cha*_*Raj 10

  1. 在cell的.h文件中放置一个弱的tableView属性,如:

    @property (weak,nonatomic)UITableView *tableView;

  2. 在cellForRowAtIndex方法中分配属性,如:

    cell.tableView = tableView;

  3. 现在,只要你需要cellPath的indexPath:

    NSIndexPath *indexPath = [cell.tableView indexPathForCell:cell];


Gev*_*yan 6

对于迅捷

let indexPath :NSIndexPath = (self.superview.superview as! UITableView).indexPathForCell(self)!
Run Code Online (Sandbox Code Playgroud)