【问题标题】:swift tableview change reloadData to insertRowsswift tableview 将 reloadData 更改为 insertRows
【发布时间】:2019-03-25 00:29:34
【问题描述】:

试图改变方法来更新数据,因为 reloadData 有滞后

let oldIns = insertCounter
insertCounter += Int(INSERT_MESSAGES) // +40
var indexPaths = [IndexPath]()
for section in (oldIns..<insertCounter) {
    indexPaths.append(IndexPath(row: 2, section: section))
}
tableView.beginUpdates()
tableView.insertRows(at: indexPaths, with: .automatic)
tableView.endUpdates()

但我有错误

更新后表格视图中包含的节数 (80) 必须等于表中包含的节数 更新前查看(40),加减部分数量 插入或删除(0 插入,0 删除)

func numberOfSections(in tableView: UITableView) -> Int {

    return min(insertCounter, Int(dbmessages.count))
}

    //---------------------------------------------------------------------------------------------------------------------------------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 5
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return RCMessages().sectionHeaderMargin
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

        return RCMessages().sectionFooterMargin
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        view.tintColor = UIColor.clear
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------
    func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {

        view.tintColor = UIColor.clear
    }

    //---------------------------------------------------------------------------------------------------------------------------------------------
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if (indexPath.row == 0) {
            return RCSectionHeaderCell.height(indexPath, messagesView: self)
        }

        if (indexPath.row == 1) {
            return RCBubbleHeaderCell.height(indexPath, messagesView: self)
        }

        if (indexPath.row == 2) {

            let rcmessage = self.rcmessage(indexPath)
            if (rcmessage.type == RC_TYPE_STATUS)   { return RCStatusCell.height(indexPath, messagesView: self)             }
            if (rcmessage.type == RC_TYPE_TEXT)     { return RCTextMessageCell.height(indexPath, messagesView: self)        }

        }

        if (indexPath.row == 3) {
            return RCBubbleFooterCell.height(indexPath, messagesView: self)
        }

        if (indexPath.row == 4) {
            return RCSectionFooterCell.height(indexPath, messagesView: self)
        }
        return 0
    }

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

        if (indexPath.row == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RCSectionHeaderCell", for: indexPath) as! RCSectionHeaderCell
            cell.bindData(indexPath, messagesView: self)
            return cell
        }

        if (indexPath.row == 1) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RCBubbleHeaderCell", for: indexPath) as! RCBubbleHeaderCell
            cell.bindData(indexPath, messagesView: self)
            return cell
        }

        if (indexPath.row == 2) {

            let rcmessage = self.rcmessage(indexPath)
            if (rcmessage.type == RC_TYPE_STATUS) {
                let cell = tableView.dequeueReusableCell(withIdentifier: "RCStatusCell", for: indexPath) as! RCStatusCell
                cell.bindData(indexPath, messagesView: self)
                return cell
            }

            if (rcmessage.type == RC_TYPE_TEXT) {
                let cell = tableView.dequeueReusableCell(withIdentifier: "RCTextMessageCell", for: indexPath) as! RCTextMessageCell
                cell.bindData(indexPath, messagesView: self)

                let numSections = self.tableView.numberOfSections
                if numSections == 1  {
                    updateTableContentInset()
                }
                return cell
            }

        }

        if (indexPath.row == 3) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RCBubbleFooterCell", for: indexPath) as! RCBubbleFooterCell
            cell.bindData(indexPath, messagesView: self)
            return cell
        }

        if (indexPath.row == 4) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RCSectionFooterCell", for: indexPath) as! RCSectionFooterCell
            cell.bindData(indexPath, messagesView: self)
            return cell
        }

        return UITableViewCell()
    }

如何在tableview 中正确插入新行? 以前是这样的

insertCounter += Int(INSERT_MESSAGES)
tableView.reloadData()

【问题讨论】:

  • return min(insertCounter, Int(dbmessages.count)) 你能解释一下吗?
  • @Manish_Nainwal 我加载到数据库的 tableview 部分(几乎没有消息)。当我向上滚动时,我想添加数据库的另一部分,insertCounter - 计算我不会添加到 tableview 中的消息数
  • 我意识到我需要添加一行,但是你能帮我添加一个新的部分吗?
  • 你必须在更新调试后返回总计数并检查更新前后的节数。
  • 和insertRow一样有一个方法insertSection

标签: swift uitableview


【解决方案1】:

这是一个非常简单的代码行:

 tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .top)

这将在表格视图的顶部插入新行。然后你会重新加载数据。这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多