我有一个名为 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/