【问题标题】:UITableView crash Unrecognized selector sent to instanceUITableView 崩溃无法识别的选择器发送到实例
【发布时间】:2016-10-14 11:51:01
【问题描述】:

我的程序因以下报告而崩溃。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fb4e1c099b0'

这是我对 tableview 方法的实现。

崩溃日志here

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (section == 0) {
            return 6;
        } 
        return 5;
    }

有人知道为什么会这样吗?

【问题讨论】:

  • else if if ????和 section 只是一个,而不是为什么你需要检查 section 是否为 0??
  • 你的 tableview 总是为 numberOfRowsInSection 返回 1
  • 请分享完整的崩溃日志。
  • 我已经修改了上面的代码,显示我只有一个部分,但它仍然崩溃。这是完整的崩溃日志。 gist.github.com/anonymous/283415d293042edc7c00d476df98d774

标签: ios iphone xcode uitableview crash


【解决方案1】:

错误消息说您正在将方法tableView:numberOfRowsInSection: 发送到不理解该方法的 UIView 对象。您的 tableview 的数据源不正确,或者您打算向 UIView 发送不同的方法。

如果您找不到与您的错误相关的代码行,您可能会共享更多代码。否则,您发布的代码只是样板文件,没有帮助。但这就是您的错误消息的含义。

【讨论】:

  • 你是对的。在界面生成器中。 tableview 数据源和委托链接到 UIView 而不是 Files 所有者。现在问题解决了。
【解决方案2】:

如果 if(section == 0) 在 else 附近只有一个节和语法错误 它会像 'else if' 中的 'if' 对待

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (section == 0) {
            return 6;
        } 
        else if (section == 1) {
            return 3;
        }
    }

【讨论】:

    【解决方案3】:
     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 2
     }
    
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         if section == 0 {
            return firstSectionArray.count
         }else if section == 1{
            return secondSectionArray.count
         }
         return 0;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多