Отставание в виде таблицы при вставке динамических ячеек и прокрутке вниз - PullRequest
0 голосов
/ 06 сентября 2018

Я создаю приложение чат-бота, в котором я добавляю различные виды ячеек в табличное представление (с динамической высотой), а после добавления я прокручиваю до дна.

Когда размер содержимого таблицы меньше по сравнению с высотой экрана, нет проблем с добавлением ячеек. Но когда размер содержимого таблицы увеличивается (за пределами экрана), добавление ячеек происходит медленно, и на моем старом iPad (iOS 10) ячейки даже не отображаются правильно (части обрезаются). Кто-нибудь может мне помочь?

Я делаю сетевой вызов, чтобы получить новое сообщение, после того, как я нажал на ячейку Результатом этого звонка я заполняю таблицу.

func ask(sessionId: String, question: String, retry : Bool, completion : @escaping CompletionBlock) {
    Alamofire.request(url!, method: .get, parameters: params, encoding: URLEncoding.default, headers: self.defaultHeaders).responseJSON { response in
            switch response.result {
            case .success(let results):
                completion(.success, results as? NSObject)
                break
            case .failure:
                completion(.error, nil)
                break
            }
        }
}

Когда вызов был успешным, и объект данных был добавлен в массив с новым сообщением, я добавляю новую ячейку в этой части:

func insertRowChatView(chatContent: [ChatMessage]) {
    if chatContent.count > self.chatMessageArray.count {
        self.chatMessageArray = chatContent

        let indexPath: IndexPath = IndexPath(row: self.chatMessageArray.count - 1, section: 0)
        self.tableView.insertRows(at: [indexPath], with: .none)
        self.tableView.reloadRows(at: [indexPath], with: .none)
        self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
}

И мой код для мобильного телефона:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if (chatMessageArray.count) > indexPath.row {
        if chatMessageArray[indexPath.row].dialogOptions != nil && chatMessageArray[indexPath.row].dialogOptions!.count > 0 {
            // Dialogoptions cell
            let cell = tableView.dequeueReusableCell(withIdentifier: "ChatDialogOptionCell", for: indexPath as IndexPath) as! ChatDialogOptionCell
            cell.delegate = self
            cell.setupCell(dialogOptions: chatMessageArray[indexPath.row].dialogOptions!, text: chatMessageArray[indexPath.row].text!, lastItem: chatMessageArray[indexPath.row].lastItem)
            return cell
        } else if chatMessageArray[indexPath.row].links != nil && chatMessageArray[indexPath.row].links!.count > 0 {
            // Links cell
            let cell = tableView.dequeueReusableCell(withIdentifier: "ChatLinkCell", for: indexPath as IndexPath) as! ChatLinkCell
            cell.delegate = self
            cell.setupCell(links: chatMessageArray[indexPath.row].links!, text: chatMessageArray[indexPath.row].text!, lastItem: chatMessageArray[indexPath.row].lastItem)
            return cell
        } else if chatMessageArray[indexPath.row].text != nil && chatMessageArray[indexPath.row].text != "" {
            // Text only
            let cell = tableView.dequeueReusableCell(withIdentifier: "ChatTextOnlyCell", for: indexPath as IndexPath) as! ChatTextOnlyCell
            cell.setupCell(text: chatMessageArray[indexPath.row].text!, askedByUser: chatMessageArray[indexPath.row].askedByUser, lastItem: chatMessageArray[indexPath.row].lastItem)
            return cell
        } else {
            // Typing indicator
            let cell = tableView.dequeueReusableCell(withIdentifier: "ChatTypingIndicatorCell", for: indexPath as IndexPath) as! ChatTypingIndicatorCell
            cell.setupCell()
            return cell
        }
    }

    let cell = UITableViewCell()
    cell.selectionStyle = UITableViewCellSelectionStyle.none
    return cell
}

Есть ли проблема с моим кодом, вызывающая эту проблему производительности?

Спасибо за помощь!

...