如何滚动到 viewdidload 时发送的最后一条消息?在 swift 4
How to scroll to the last message sent when viewdidload? in swift 4
目前,我已经试过了
func scrollToBottom(){
DispatchQueue.global(qos: .background).async {
let indexPath = IndexPath(item: self,messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath,at:.bottom,animated:true)
}
}
不过好像不行,正在输出
Thread 16: ESC_BAD_ACCESS (code=1, address=0xfffffffffffffffc)
UITableView.scrollToRow(at:at:animated:) must be used from main thread only
我在 viewdidload 中设置委托和数据源后立即调用该函数。
首先:请提供您的代码作为文本而不是图像。
现在你的问题是,你正试图滚动到全局队列中的最后一行,但所有 UI-更新都必须在主线程中发生,所以只需删除 DispatchQueue,或者如果你是不在主线程中,可以写 DispatchQueue.main
而不是 global
例如:
DispatchQueue.main.async {
//code Here
}
但我认为您根本不需要 DispatchQueue,因此请尝试完全忽略它。
它喜欢在这个主线程上工作而不是在后台工作,因为它与 UI
试试这个代码:
func scrollToBottom() {
DispatchQueue.main.async {
let indexPath = IndexPath(item: self.messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
在“.async”闭包中编写代码会启动一个新线程,错误消息字面上告诉您不要这样做。
删除 DispatchQueue
项,仅此而已。
请 post 文本、描述和所有帮助社区的东西。
You don't need to add DispatchQueue:
let indexPath = IndexPath(item: self.messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
And if you want DispatchQueue then add main queue like that:
DispatchQueue.main.async {
let indexPath = IndexPath(item: self.messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
可能是索引路径必须越界,请尝试在滚动前检查行数。还要在主线程上执行所有操作。
听起来像是时间问题,您说您是从 viewDidLoad 调用它,请尝试将其移至 viewDidAppear 或 viewDidLayoutSubviews。在操作 UI 时也从后台线程中删除,这需要在主线程上进行。
目前,我已经试过了
func scrollToBottom(){
DispatchQueue.global(qos: .background).async {
let indexPath = IndexPath(item: self,messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath,at:.bottom,animated:true)
}
}
不过好像不行,正在输出
Thread 16: ESC_BAD_ACCESS (code=1, address=0xfffffffffffffffc)
UITableView.scrollToRow(at:at:animated:) must be used from main thread only
我在 viewdidload 中设置委托和数据源后立即调用该函数。
首先:请提供您的代码作为文本而不是图像。
现在你的问题是,你正试图滚动到全局队列中的最后一行,但所有 UI-更新都必须在主线程中发生,所以只需删除 DispatchQueue,或者如果你是不在主线程中,可以写 DispatchQueue.main
而不是 global
例如:
DispatchQueue.main.async {
//code Here
}
但我认为您根本不需要 DispatchQueue,因此请尝试完全忽略它。
它喜欢在这个主线程上工作而不是在后台工作,因为它与 UI
试试这个代码:
func scrollToBottom() {
DispatchQueue.main.async {
let indexPath = IndexPath(item: self.messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
在“.async”闭包中编写代码会启动一个新线程,错误消息字面上告诉您不要这样做。
删除 DispatchQueue
项,仅此而已。
请 post 文本、描述和所有帮助社区的东西。
You don't need to add DispatchQueue:
let indexPath = IndexPath(item: self.messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
And if you want DispatchQueue then add main queue like that:
DispatchQueue.main.async {
let indexPath = IndexPath(item: self.messageArrat.count-1, section: 0)
self.messageTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
可能是索引路径必须越界,请尝试在滚动前检查行数。还要在主线程上执行所有操作。
听起来像是时间问题,您说您是从 viewDidLoad 调用它,请尝试将其移至 viewDidAppear 或 viewDidLayoutSubviews。在操作 UI 时也从后台线程中删除,这需要在主线程上进行。