Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么重新加载数据后调用scrollToRow会导致崩溃?_Ios_Swift_Uitableview_Reloaddata - Fatal编程技术网

Ios 为什么重新加载数据后调用scrollToRow会导致崩溃?

Ios 为什么重新加载数据后调用scrollToRow会导致崩溃?,ios,swift,uitableview,reloaddata,Ios,Swift,Uitableview,Reloaddata,当在数据之后调用scrollToRow时,重新加载数据会导致崩溃。我必须在DispatchQueue.main.async中编写此代码,但是,为什么?当我在主队列中时,为什么要说切换到主队列 self.tableView.reloadData() print(Thread.current). // It is main self.tableView.scrollToRow(at: indexPathToScroll, at: .top, animated: false) print(Threa

当在数据之后调用scrollToRow时,重新加载数据会导致崩溃。我必须在
DispatchQueue.main.async
中编写此代码,但是,为什么?
当我在主队列中时,为什么要说切换到主队列

self.tableView.reloadData()
print(Thread.current).  // It is main
self.tableView.scrollToRow(at: indexPathToScroll, at: .top, animated: false)
print(Thread.current).  // It is main
错误是:

_ContentOffsetForScrollingToRowatineXPath:atScrollPosition:usingPresentationValues:: 第(20)行超出第(0)节的边界(20)

问题是这样解决的:

DispatchQueue.main.async(execute: {
self.tableView.reloadData()
    print(Thread.current).  // It is main
    self.tableView.scrollToRow(at: indexPathToScroll, at: .top, animated: false)
    print(Thread.current).  // It is main
})

当我在第一个代码中打印时,它是在主线程中运行的。为什么
DispatchQueue.main.async
起了作用

我找到的最简单的答案是

虽然每个应用程序都只有一个主线程,但在这个主线程上执行许多不同的队列是可能的

某些API不仅依赖于在主线程上运行,还依赖于在主队列上运行,检查当前队列比检查当前线程更安全