Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
741 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...