Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField subview of UITableViewCell, get indexPath of cell

I have added a UITextField as a subview of a UITableViewCell. Then I have added a target and selector so that I can know when UIControlEventEditingChanged. This works great, but I would like you know the indexPath of the cell that the UITextField is in, as it could be added to any number of cells.

Is this possible? Basically I want to find the parent view which is a UITableViewCell.

like image 318
Nic Hubbard Avatar asked Mar 06 '12 03:03

Nic Hubbard


People also ask

How do I get indexPath from cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

Is it possible to add UITableView within a UITableViewCell?

Implementation of adding a table view inside the cell of UItableview aka, Nested Table View. Used the Power of Autosizing table view and delegate to achieve the expansion and collapse of the cell height.

What does indexPath row return?

So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.


2 Answers

Call [UIView superview] on the field to get the cell that it's in, then call [UITableView indexPathForCell:] on the cell to get the index path.

UPDATE: on iOS 7 you need to call superview on that view too (extra layer of views); here's a category on UITableView that should work independent of iOS version:

@interface UITableView (MyCoolExtension) 

- (NSIndexPath *)indexPathForCellContainingView:(UIView *)view;

@end

@implementation UITableView (MyCoolExtension)

- (NSIndexPath *)indexPathForCellContainingView:(UIView *)view {
    while (view != nil) {
        if ([view isKindOfClass:[UITableViewCell class]]) {
            return [self indexPathForCell:(UITableViewCell *)view];
        } else {
            view = [view superview];
        }
    }

    return nil;
}

@end
like image 143
Ertebolle Avatar answered Oct 03 '22 22:10

Ertebolle


Instead of recursively trying to find the superview, there's a simpler solution:

Swift

func indexPathForCellContainingView(view: UIView, inTableView tableView:UITableView) -> NSIndexPath? {
    let viewCenterRelativeToTableview = tableView.convertPoint(CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)), fromView:view)
    return tableView.indexPathForRowAtPoint(viewCenterRelativeToTableview)
}

Objective-C

- (NSIndexPath *)indexPathForCellContainingView:(UIView *)view inTableView:(UITableView *)tableView {
    CGPoint viewCenterRelativeToTableview = [tableView convertPoint:CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds)) fromView:view];
    NSIndexPath *cellIndexPath = [tableView indexPathForRowAtPoint:viewCenterRelativeToTableview];
    return cellIndexPath
}

I made this into a Gist for the Swift hearted

like image 40
Daniel Galasko Avatar answered Oct 03 '22 20:10

Daniel Galasko