我正在制作一个聊天机器人应用程序,其中我在表视图中添加了不同类型的单元格(具有动态高度),添加后,我滚动到底部。
当表格视图的内容大小小于屏幕高度时,添加单元格没有问题。但是,当tableview内容的大小必须增加(在屏幕外部)时,添加单元格的时间就很漫长,在我的旧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)
}
}
还有我的cellforrowatindex路径代码:
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
}
我的代码是否存在问题,导致此性能问题?
感谢您的帮助!