为什么在Interface Builder中向UITableViewCell子类添加手势识别器会使应用程序崩溃?

Can*_*ğlu 5 objective-c uitableview uigesturerecognizer ios

我有一个自定义UITableViewCell子类,我正在尝试使用Interface Builder将一个捏手势识别器添加到其中一个视图中.我的应用崩溃了:

2016-09-11 17:37:22.425 MYAPPNAME[4619:1284144] *** Assertion failure in -[ULFeedView _dequeueReusableViewOfType:withIdentifier:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UITableView.m:6539

我尝试了不同的手势识别器(例如点击识别器)和不同的子视图,但它们崩溃了我的应用程序.

一个有趣的观察:如果我以编程方式将识别器添加到视图中,它不会崩溃awakeFromNib.

以下是一些可能相关的方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section != SECTION_CONTENT){
        return; //index path section is NOT equal to SECTION CONTENT for the cell in question, so it will always return.
    }
    ...
 }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case SECTION_MAP:
        {
            ULMapCell *cell = [tableView dequeueReusableCellWithIdentifier:@"map" forIndexPath:indexPath];
            return cell; //the cell in question is this one, so it will always return this cell.
        }
     ...
}
Run Code Online (Sandbox Code Playgroud)

更新:注册nib没有问题.在手势识别器之前,它已经完美地工作了.请停止告诉我如何为表视图注册nib,我已经知道这是一个高级iOS开发人员.

更新2:我确认它只在我通过Interface Builder添加它时才会出现,如果我以编程方式将其添加到任何地方都没有问题.

为什么会这样?

nil*_*ils 8

笔尖中的对象按层次结构组织.顶层通常只有一个对象:根视图(在您的情况下是单元格).但是,nib可以包含多个顶级对象.事实上,手势识别器作为顶级对象添加到笔尖.

任何使用多个顶级对象加载nib的代码都需要知道如何处理它.例如,为a加载nib的代码UITableViewCell可以:

  1. 在顶级对象数组中查找单元格对象
  2. 确保没有其他单元格(因为添加一个可供选择的启发式方法是不切实际的)
  3. 忽略所有顶级UIGestureRecognizers,因为它们被添加到的视图保留
  4. 确保顶级对象数组中没有其他内容

不幸的是,Apple在加载UITableViewCellUICollectionViewCellnibs 时选择处理多个顶级对象的方式是抛出异常.这就是我们无法在Interface Builder中向单元格添加手势识别器的原因.


Dav*_*ong 0

在 cellforrow 方法中尝试这个,案例 SECTION_MAP

[tableView registreNib:[UINib nibWithName@"ULMapCell" bundle:nil] forCellReuseIdentifier@"map"];

ULMapCell *cell = (ULMapCell*)[tableView dequeueReusableCellWithIdentifier:@"map"];
if(!cell){
 //alloc and init the cell
}
return cell;
Run Code Online (Sandbox Code Playgroud)

我更喜欢在 CustomCell 本身中添加手势并设置委托方法。

希望这可以帮助。