быстрое изменение табличного представления reloadData к insertRows - PullRequest
0 голосов
/ 25 марта 2019

пытается изменить метод для обновления данных, потому что с 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 голосов
/ 25 марта 2019

Это довольно простая строка кода:

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

Это вставит новую строку вверху табличного представления. Тогда вы перезагрузите данные. Это должно решить проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...