ios - 即使我在清空数组后重新加载数据,Tableview cellForRowAt 也会崩溃?

标签 ios swift tableview

我一直在寻找如何解决我的应用程序中最大的问题之一,但即使我遵循此 iOS/Swift: tableView cellForRowAtIndexPath crash,我的问题仍然存在.根据我提供的链接,在我清空一个数组后,一定要记得重新加载数据。但是,问题依然存在,不知道如何解决。

一些登录 Crashlytics ...

我的 tableview cellForRowAt 函数,我也在 Storyboard中设置了单元格标识符。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Crashed somewhere here
    let currentRow = displayMainCell[indexPath.row] 

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MainCell else { return UITableViewCell() } 


    if currentRow.id == "wotd" {
        cell.contentLabel.font = UIFont(name: "Helvetica Neue", size: 22)
    } else {
        cell.contentLabel.font = UIFont(name: "Helvetica Neue", size: 13)
    }

    cell.titleLabel.text = currentRow.title
    cell.contentLabel.text = currentRow.content

    return cell
}

已添加: 我的 didSelectRowAt 在不同的 View Controller 中也有问题。这是实现。

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let currentRow = wordLists[indexPath.row]

    textField.text = currentRow

    let currentWordToSearch = currentRow.condenseWhitespace().lowercased()
    indicator.startAnimating()

    saveWordToHistory(currentWordToSearch)
    fetchWordFromDB(currentWordToSearch)
    wordChange(wordToEncode: currentWordToSearch, uuid: getDeviceID())

    wordLists = []
    tableView.reloadData()
    setupDatabase(suggWords: currentWordToSearch)
    tableView.isHidden = true

    textField.resignFirstResponder()
}

最佳答案

检查以下函数是否返回常量值。它应该是这样的:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayName.count;
}

如果你已经这样做了。我建议您尝试以下代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MainCell else { return UITableViewCell() } 

if arrayName.count != 0 {
let currentRow = displayMainCell[indexPath.row] 

if currentRow.id == "wotd" {
    cell.contentLabel.font = UIFont(name: "Helvetica Neue", size: 22)
} else {
    cell.contentLabel.font = UIFont(name: "Helvetica Neue", size: 13)
}

cell.titleLabel.text = currentRow.title
cell.contentLabel.text = currentRow.content
}
return cell
}

关于ios - 即使我在清空数组后重新加载数据,Tableview cellForRowAt 也会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47130864/

相关文章:

iOS UITableView 自定义 RowHeightt

快速重新加载单元格而不重新加载整个 tableView

ios - 使用 ReactiveCocoa 查看验证

ios - 如何检测 collectionview 超出屏幕并将其返回到其初始滚动位置?

iOS 8 小部件无法在 iPhone 6+ 8.0.2 上运行。 xCode 6.1

ios - 多次调用 estimatedHeightForRowAtIndexPath 导致不稳定的 uitableview

ios - 从 iOS 创建 getStream 用户

ios - 在 playerItem 的最后一秒暂停 AVPlayer

android - 打开其中之一后,通知中心中的通知消失

ios - Swift:UICollectionView - 单元格之间的固定空间