我在UITableViewCell中有一个UITextField。当用户尝试在此文本字段中输入一些文本时,app会将该单元格滚动到UITableView的顶部。这是代码:
#pragma mark - UITextField Delegates
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
UITableViewCell *QCELL = (UITableViewCell *)[[[[textField superview]superview]superview]superview];
UITableView *QTable = (UITableView *)[[QCELL superview] superview];
NSIndexPath*path = = [QTable indexPathForCell:QCELL];
return true;
}
和例外
2014-09-11 11:12:50.604 InspectTHIS[9401:348011] -[UIView indexPathForCell:]: unrecognized selector sent to instance 0x7c2e8b30
2014-09-11 11:12:50.608 InspectTHIS[9401:348011] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView indexPathForCell:]: unrecognized selector sent to instance 0x7c2e8b30'
我在Views Layer中搜索后发现了这个
////////// iOS 8 //////////
<UIView: 0x792aabd0; frame = (0 0; 768 1023); opaque = NO; autoresize = W+H; layer = <CALayer: 0x792aac40>>
<UITableViewCellContentView: 0x79758340; frame = (0 0; 768 699); opaque = NO; gestureRecognizers = <NSArray: 0x79758530>; layer = <CALayer: 0x797583b0>>
<QuestionCell: 0x79757ed0; baseClass = UITableViewCell; frame = (0 0; 768 699); autoresize = W; layer = <CALayer: 0x79758190>>
<UITableViewWrapperView: 0x7967bc50; frame = (0 0; 768 750); gestureRecognizers = <NSArray: 0x7967bef0>; layer = <CALayer: 0x7967be30>; contentOffset: {0, 0}; contentSize: {768, 750}>
<UITableView: 0x78c99600; frame = (0 244; 768 750); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7967b9c0>; layer = <CALayer: 0x7967ae00>; contentOffset: {0, 0}; contentSize: {768, 699}>
<UIView: 0x7967abd0; frame = (0 0; 768 1024); autoresize = RM+BM; layer = <CALayer: 0x7967ac40>>
//////// iOS 7 ////
<UIView: 0x11fbbbf0; frame = (0 0; 768 1023); opaque = NO; autoresize = W+H; layer = <CALayer: 0x11fbbc50>>
<UITableViewCellContentView: 0x11f832a0; frame = (0 0; 768 699); opaque = NO; gestureRecognizers = <NSArray: 0x11f8dd40>; layer = <CALayer: 0x11faa6f0>>
<UITableViewCellScrollView: 0x11fc4090; frame = (0 0; 768 699); autoresize = W+H; gestureRecognizers = <NSArray: 0x11f9c830>; layer = <CALayer: 0x11f978f0>; contentOffset: {0, 0}>
<QuestionCell: 0x11fa2090; baseClass = UITableViewCell; frame = (0 0; 768 699); autoresize = W; layer = <CALayer: 0x11fa1c90>>
<UITableViewWrapperView: 0xced1860; frame = (0 0; 768 750); autoresize = W+H; layer = <CALayer: 0xced18d0>>
<UITableView: 0xd958800; frame = (0 244; 768 750); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0xced1560>; layer = <CALayer: 0xced0eb0>; contentOffset: {0, 0}>
<UIView: 0xced0c30; frame = (0 0; 768 1024); autoresize = RM+BM; layer = <CALayer: 0xced0c90>>
正如你在iOS 8中看到的那样,缺少UITableViewCellScrollView,而且我不能像在iOS 7中那样上去。所以我改变了这样的代码。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSIndexPath*path;
UITableView *QTable;
CGFloat version =[[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 7.0)
{
UITableViewCell *QCELL = (UITableViewCell *)[[[[textField superview]superview]superview]superview];
QTable = (UITableView *)[[QCELL superview] superview];
path = [QTable indexPathForCell:QCELL];
}
else if(version >= 8.0)
{
UITableViewCell *QCELL = (UITableViewCell *)[[[textField superview]superview]superview];
QTable = (UITableView *)[[QCELL superview] superview];
path = [QTable indexPathForCell:QCELL];
}
NSIndexPath*path = = [QTable indexPathForCell:QCELL];
return true;
}
答案 0 :(得分:2)
我稍微更改了原始代码并提出了这个问题:
我做了这个更改,因为当版本为8时我遇到了问题,因为&gt; = 7被触发了。我需要if之外的变量。
CGFloat version =[[[UIDevice currentDevice] systemVersion] floatValue];
UITableViewCell *cell;
UITableView *table;
if ((version > 7) && (version < 8))
{
UITableViewCell *cell = (UITableViewCell*)[[[sender superview] superview]superview];
table = (UITableView *)cell.superview.superview;
}
else if(version > 8)
{
UITableViewCell *cell = (UITableViewCell*)[[sender superview] superview];
table = (UITableView *)cell.superview.superview;
}
答案 1 :(得分:1)
同样,我在UITableViewCell中有一个按钮。这对你有用。这里buttonView意味着我的UIButton实例
对于iOS 8
UITableViewCell *cell = (UITableViewCell*)[buttonView superview];
给了我UITableViewCell实例 和往常一样的iOS 7
UITableViewCell *cell = [buttonView superview]superview];
给了我UITableViewCell实例
和
[QTable indexPathForCell:cell];
会给你NSIndexPath(这部分在给定的答案中没问题。)
答案 2 :(得分:1)
这是遍历视图层次结构而不是使用单元格本身的委托方法所能获得的。
让Cell本身响应textview的委托。让单元格拥有自己的协议(委托方法),让你的TableViewController成为它的委托。
然后,子类UITableViewCell类中的代码将如下所示:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.delegate textFieldDidBeginEditingForCell:self];
return true;
}
然后在你的TableViewController中:
- (void)textFieldDidBeginEditingForCell:(YourCustomCell*)cell
{
NSIndexPath *path = [QTable indexPathForCell:cell];
...
}