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

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

问题描述

我已经添加了 UITextField 作为 UITableViewCell 的子视图。然后我添加了一个 target selector ,这样我可以知道 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子视图,获取单元格的indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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