iOS 8:-[UITableViewWrapperView textField]:发送到实例的无法识别的选择器
你好:我一直在iOS 6、7和现在的8(beta 5)上测试我的应用程序。我的带有自定义iOS 8:-[UITableViewWrapperView textField]:发送到实例的无法识别的选择器,ios,objective-c,uitableview,ios8,Ios,Objective C,Uitableview,Ios8,你好:我一直在iOS 6、7和现在的8(beta 5)上测试我的应用程序。我的带有自定义UITableViewCells的UITableView在6号和7号工作正常。然而,在iOS 8上,当我试图访问单元格的子视图(文本字段)时,我会崩溃 我知道在iOS 7中,在单元的层次结构中有另一个视图。奇怪的是,iOS 8中的情况似乎并非如此。以下是我使用的代码: //Get the cell CustomCell *cell = nil; //NOTE: GradingTabl
UITableViewCell
s的UITableView
在6号和7号工作正常。然而,在iOS 8上,当我试图访问单元格的子视图(文本字段)时,我会崩溃
我知道在iOS 7中,在单元的层次结构中有另一个视图。奇怪的是,iOS 8中的情况似乎并非如此。以下是我使用的代码:
//Get the cell
CustomCell *cell = nil;
//NOTE: GradingTableViewCell > UITableViewCellScrollView (iOS 7+ ONLY) > UITableViewCellContentView > UIButton (sender)
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
cell = (CustomCell *)sender.superview.superview;
} else {
cell = (CustomCell *)sender.superview.superview.superview;
}
//Get the cell's index path
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//etc.
NSLog(@"%@", cell.textField); //<---Crashes here
我进一步研究了一下,发现:
将else
语句更改为与前一行相同可以消除崩溃,应用程序工作正常(使用类似于iOS 6的sender.superview.superview)。
这对我来说毫无意义。苹果是否将
UITableViewCell
s的层次结构恢复为iOS 6的层次结构,还是我遗漏了什么?谢谢 不同的iOs版本有不同的UITableView
或UITableViewController
实现,作为一个简单的解决方案,您必须迭代superview
,直到找到所需的视图类,而不是依赖它作为第N个superview。我遇到了同样的问题。这里有一个更可靠的方法:
UITextField* textField = (UITextField*)sender;
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:[self.tableView convertPoint:textField.center fromView:textField.superview]];
无论UITableViewCell的基础视图层次结构如何,这都将起作用 我在iOS8上也遇到了同样的问题,即从子视图通过superview获取UITableViewCell。这就是我为iOS7和iOS8提供的支持
-(void)thumbTapped:(UITapGestureRecognizer*)recognizer {
NSLog(@"Image inside a tableview cell is tapped.");
UIImageView *mediaThumb = (UIImageView*)recognizer.view;
UITableViewCell* cell;
// get the index of container cell's row
// bug with ios7 vs ios8 with superview level!! :(
/*** For your situation the level of superview will depend on the design structure ***/
// The rule of thumb is for ios7 it need an extra superview
if (SYSTEM_VERSION_LESS_THAN(@"8.0")) { // iOS 7
cell = (UITableViewCell*)mediaThumb.superview.superview.superview.superview;
} else { // iOS 8
cell = (UITableViewCell*)mediaThumb.superview.superview.superview;
}
}
为了澄清这一点,我在cellForRowAtIndexPath中指定了轻触手势识别器。故事板的原始设计非常复杂,有许多子视图、按钮。。等等
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MediaCell" forIndexPath:indexPath];
.......
UIImageView *mediaImage = ............
.....................................
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thumbTapped:)];
[mediaImage addGestureRecognizer:tapGestureRecognizer];
return cell;
}
假设视图层次结构是一个坏主意,因为它会发生变化(如您所见)。更好的解决方案是浏览
superview
链接,直到找到所需的类。不管发生什么,代码都会工作。接受这个答案是因为它比遍历SuperView更简短、更优雅。谢谢谢谢你的回答。这是可行的,但是@geraldWilliam的答案更简洁,不需要迭代+1尽管如此。@rebello95哦,谢谢你的支持,我同意另一种解决方案对你来说更好,只要记住一个在超级视图中迭代的选项,有时会很方便。这比它应该的要复杂得多。另外,正如在其他答案中提到的,您不应该手动指定要增加多少SuperView。动态地做。是的,我同意。我会试试那个解决方案。谢谢
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MediaCell" forIndexPath:indexPath];
.......
UIImageView *mediaImage = ............
.....................................
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thumbTapped:)];
[mediaImage addGestureRecognizer:tapGestureRecognizer];
return cell;
}