在UITableViewCell iOS7中搜索超级视图

And*_*cci 3 uitableview ios ios7

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

-(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];
Run Code Online (Sandbox Code Playgroud)

这给了我正确的结果,我在这里看到了一个类似的解决方案 ,它也导航了视图层次结构.我想知道这是否是唯一的解决方案,或者是否有更合适的东西不涉及所有可用单元的循环.

lan*_*nbo 5

试试这个,不需要遍历视图

-(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);
}
Run Code Online (Sandbox Code Playgroud)