swift tableview将reloadData更改为insertRows

时间:2019-03-25 00:29:34

标签: swift uitableview

尝试更改方法以更新数据,因为使用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()

1 个答案:

答案 0 :(得分:0)

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

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

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