How to get UITableViewCell indexPath from the Cell?

后端 未结 11 1605
野趣味
野趣味 2020-12-07 20:11

How do I, from a cell, get its indexPath in a UITableView?

I\'ve searched around stack overflow and google, but all the information is on t

相关标签:
11条回答
  • 2020-12-07 20:17

    To address those who say "this is a bad idea", in my case, my need for this is that I have a button on my UITableViewCell that, when pressed, is a segue to another view. Since this is not a selection on the cell itself, [self.tableView indexPathForSelectedRow] does not work.

    This leaves me two options:

    1. Store the object that I need to pass into the view in the table cell itself. While this would work, it would defeat the point of me having an NSFetchedResultsController because I do not want to store all the objects in memory, especially if the table is long.
    2. Retrieve the item from the fetch controller using the index path. Yes, it seems ugly that I have to go figure out the NSIndexPath by a hack, but it's ultimately less expensive than storing objects in memory.

    indexPathForCell: is the correct method to use, but here's how I would do it (this code is assumed to be implemented in a subclass of 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;
    }
    

    P.S. My real code does access all this from the table view, but I'm giving an example of why someone might want to do something like this in the table cell directly.

    0 讨论(0)
  • 2020-12-07 20:21

    For swift

    let indexPath :NSIndexPath? = (self.superview.superview as! UITableView)?.indexPathForCell(self)
    
    0 讨论(0)
  • 2020-12-07 20:21

    On iOS 11.0, you can use UITableView.indexPathForRow(at point: CGPoint) -> IndexPath?:

    if let indexPath = tableView.indexPathForRow(at: cell.center) {
        tableView.selectRow
        (at: indexPath, animated: true, scrollPosition: .middle)
    }
    

    It gets the center point of the cell and then the tableView returns the indexPath corresponding to that point.

    0 讨论(0)
  • 2020-12-07 20:21

    Try with this from your UITableViewCell:

    NSIndexPath *indexPath = [(UITableView *)self.superview.superview indexPathForCell:self];
    
    0 讨论(0)
  • 2020-12-07 20:24

    try this (it only works if the tableView has only one section and all cells has equal heights) :

    //get current cell rectangle relative to its superview
    CGRect r = [self convertRect:self.frame toView:self.superview];
    
    //get cell index
    int index = r.origin.y / r.size.height;
    
    0 讨论(0)
  • 2020-12-07 20:28

    The answer to this question actually helped me a lot.

    I used NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

    The sender in my case was a UITableViewCell and comes from the prepareForSegue method.

    I used this because I did not have a TableViewControllerbut i had UITableView property outlet

    I needed to find out the title of the Cell and hence needed to know the indexPath of it.

    Hope this helps anyone!

    0 讨论(0)
提交回复
热议问题