ios - 在 UITableViewCell iOS7 中搜索 super View

标签 ios uitableview ios7

我有一个名为 EditableTableViewCell 的自定义 UItableViewCell 类,它(除其他外)在其 contentView< 中添加了一个 UITextField/。在 Textfield 委托(delegate)方法中,我试图检测它属于哪个部分。像这样:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    UIView* contentView =[textField superview];
    //traverse up in the view hierarchy until we find the cell's content view.
    while (![contentView isKindOfClass:[EditableTableViewCell class]] || contentView.superview == nil) {
        contentView = [contentView superview];
    }
    EditableTableViewCell *thiscell = (EditableTableViewCell*)contentView;
    NSIndexPath *indexpath =[[self tableView]indexPathForRowAtPoint:thiscell.center];

这给了我正确的结果,我看到了类似的解决方案 here这也导航 View 层次结构。我想知道这是否是唯一的解决方案,或者是否有更合适的方法不涉及对所有可用单元格进行循环。

最佳答案

试试这个,不需要遍历 View

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    UIView* contentView =[textField superview];
    CGPoint center = [self.tableView convertPoint:textField.center fromView:contentView];
    NSIndexPath *indexpath =[[self tableView] indexPathForRowAtPoint:center];
    NSLog(@"indexPath:%@", indexpath);
}

关于ios - 在 UITableViewCell iOS7 中搜索 super View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023303/

相关文章:

ios - 使用 UINavigationController 的 initWithNavigationBarClass :toolbarClass: 的正确方法

ios - CKContainer accountStatusWithCompletionHandler 返回错误值

ios - UIView 中的 Swift TableView 不显示数据

swift - 如何在侧面菜单表中快速添加 View

ios - 设置工具栏颜色和导航栏后退按钮

ios - 如何在不使用 iOS7 弃用方法的情况下获取 AirPlay 设备名称

ios - 覆盖 initWithEntity :insertIntoManagedObjectContext:

ios - 为 UITableView 强制两指滚动,但禁用一指滚动

ios - 图像未立即在表格 View 中加载

ios - TestFlight Beta 测试 - 我们能否为不同的 1000 名外部测试人员提供同一应用程序的多个版本