OGeek|极客世界-中国程序员成长平台

标题: ios - 当 UISearchBar 处于事件状态时,滚动到 UITableView 应用程序会崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:54
标题: ios - 当 UISearchBar 处于事件状态时,滚动到 UITableView 应用程序会崩溃

我的应用程序运行良好,但是突然当我单击 UISearchBar 并且向下滚动 UITableView 时没有输入,然后应用程序崩溃并出现以下错误: Thread1 : EXC_BAD_INSTRUCTION

但是当我点击 UISearchBar 并输入一些文本后,当我向下滚动 UITableView 时,应用程序运行正常,它不会崩溃。

最后,当我点击 UISearchBar 并输入一些文本并删除所有这些文本时,之后当我向下滚动 UITableView 时,应用程序工作正常,它不会崩溃。

此函数发生错误:

 func tableView(historyTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = historyTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell;
        //self.historyTableView.scrollEnabled = true
        if(searchActive){
//...Error occurs over here ==>> Thread1 : EXC_BAD_INSTRUCTION
            cell.textLabel?.text = filtered[indexPath.row]  
        } else {
            cell.textLabel?.text = data[indexPath.row]
        }

        return cell;
    }



Best Answer-推荐答案


我的猜测是,当您开始主动搜索时,您并没有重新加载表格。我还猜测 nunberOfRowsInSection: 不处理主动搜索的情况。如果这些都是真的,那么只要您滚动到大于 filtered.count

的行,调用 filtered[indexPath.row] 就会使您的应用崩溃

关于ios - 当 UISearchBar 处于事件状态时,滚动到 UITableView 应用程序会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35645261/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4