Swift 插入动态单元格并滚动到底部时,Tableview会变慢
我正在制作一个聊天机器人应用程序,在其中我在一个tableview中添加不同种类的单元格(具有动态高度),添加后,我将滚动到底部 当tableview的内容大小小于屏幕高度时,添加单元格没有问题。但是当tableview内容的大小(在屏幕外)必须增加时,单元格的添加就会滞后,在我的旧iPad(iOS 10)上,单元格甚至不能正确显示(部分被切断)。有人能帮我吗 在我点击了一个单元格选项之后,我通过网络呼叫来获取新消息。根据调用的结果,我填充tableviewSwift 插入动态单元格并滚动到底部时,Tableview会变慢,swift,performance,uitableview,smooth-scrolling,reloadrows,Swift,Performance,Uitableview,Smooth Scrolling,Reloadrows,我正在制作一个聊天机器人应用程序,在其中我在一个tableview中添加不同种类的单元格(具有动态高度),添加后,我将滚动到底部 当tableview的内容大小小于屏幕高度时,添加单元格没有问题。但是当tableview内容的大小(在屏幕外)必须增加时,单元格的添加就会滞后,在我的旧iPad(iOS 10)上,单元格甚至不能正确显示(部分被切断)。有人能帮我吗 在我点击了一个单元格选项之后,我通过网络呼叫来获取新消息。根据调用的结果,我填充tableview func ask(sessionId
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
}
}
}
当调用成功,并且dataobject被添加到包含新消息的数组中时,我将在本部分中添加新单元格:
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)
}
}
和我的cellforrowatindexpath代码:
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
}
我的代码是否存在导致此性能问题的问题
谢谢你的帮助 你什么时候叫“问”func?共享“cellforRowAtIndexPath”功能代码。我在开始时调用“ask”功能,并在选择单元格中的一个选项(按钮)时调用该功能。我将该选项发送到后端,并返回结果。在那之后,我添加一个新的单元格,并得到这个结果。我添加了cellforrow函数。:(请添加setupCell()源代码