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
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:
NSFetchedResultsController
because I do not want to store all the objects in memory, especially if the table is long.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.
For swift
let indexPath :NSIndexPath? = (self.superview.superview as! UITableView)?.indexPathForCell(self)
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.
Try with this from your UITableViewCell:
NSIndexPath *indexPath = [(UITableView *)self.superview.superview indexPathForCell:self];
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;
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 TableViewController
but 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!