I'm making a chatbot app, in which I'm adding different kind of cells in a tableview (with dynamic height), and after adding, I'm scrolling to the bottom.
When the content size of the tableview is smaller compared to the height of the screen, there's no problem with adding the cells. But when the tableview content size has to grow (outside the screen), adding of the cells is laggy, and on my old iPad (iOS 10), cells aren't even displayed right (parts are cut off). Could anyone help me?
I do a network call to get the new message, after I have clicked on a cell option. With the result of that call, I fill the tableview.
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
}
}
}
When the call was successful, and the dataobject was added to a array with the new message in it, I'm adding the new cell in this part:
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)
}
}
And my cellforrowatindexpath code:
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
}
Is there a problem with my code, causing this performance problem?
Thanks for helping out!