UITableViewCell的UITextField子视图,获取cell的indexPath [英] UITextField subview of UITableViewCell, get indexPath of cell

查看:32
本文介绍了UITableViewCell的UITextField子视图,获取cell的indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个 UITextField 作为 UITableViewCell 的子视图.然后我添加了一个 targetselector 以便我可以知道何时 UIControlEventEditingChanged.这很好用,但我希望您知道 UITextField 所在单元格的 indexPath,因为它可以添加到任意数量的单元格中.

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.

这可能吗?基本上我想找到父视图,它是一个 UITableViewCell.

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

推荐答案

在字段上调用 ​​[UIView superview] 获取它所在的单元格,然后调用 [UITableView indexPathForCell:] 在单元格上获取索引路径.

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.

更新:在 iOS 7 上,您也需要在该视图上调用 superview(额外的视图层);这是 UITableView 上的一个类别,它应该独立于 iOS 版本工作:

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

这篇关于UITableViewCell的UITextField子视图,获取cell的indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆