致命错误:刷新表视图时索引超出范围

时间:2016-11-19 21:41:33

标签: ios swift3 tableview pull-to-refresh

当拉到刷新时,我发现这个奇怪的应用程序崩溃。

我的代码如下:

var posts: [Posts] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // refreshControl -> pull to refresh handler
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self,
                             action: #selector(Main_TVC.getData),
                             for: UIControlEvents.valueChanged)
    self.refreshControl = refreshControl

    getData()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
                                             for: indexPath) as! PostsTableViewCell

    cell.titleLabel.text = posts[indexPath.row].postTitle
    cell.bodyLabel.text = posts[indexPath.row].postBody

    return cell
}

func getData() {
    self.posts.removeAll()

    // queries the backend and fills the data - sorry for code omission

    refresh()

}


func refresh() {
    self.tableView.reloadData()
    self.refreshControl?.endRefreshing()
}

该应用程序运行正常,即使我下拉刷新,一切都运行完美,但如果长时间下拉刷新,如拉下几乎击中屏幕的底部,应用程序崩溃并提示以下错误:

致命错误:索引超出范围

就行了

cell.titleLabel.text = posts [indexPath.row] .postTitle

如果我按如下方式打印帖子计数和indexPath:

打印(" posts.count =(posts.count)") 打印(" indexPath.row =(indexPath.row)")

当我以正常方式拉下来时,它会打印出正确的数据,但是如果我拉下来就像一个长时间的拉动,如果它在崩溃时提示它,则通过整个屏幕

posts.count = 0

indexPath.row = 2

这种事情从未发生在我使用refreshControl的地方,就像我在这里使用的那样。

希望我的信息是可以理解的,主要是长期刷新问题。

2 个答案:

答案 0 :(得分:8)

你的问题是你在getData中做的第一件事就是删除self.posts中的所有帖子,但你没有(大概是因为缺少代码)重新加载表格视图,所以现在数组中的帖子数(0)以及tableview *认为在数组中的帖子数(不为零)是不同的,因此您会遇到数组边界崩溃。

reloadData之后调用self.posts.removeAll()可以解决问题,但会导致表格查看'闪烁'因为它刷新了空,然后用新数据重新绘制。

由于您还没有显示getData的完整代码,因此我无法提供您需要的确切代码,但它应该是这样的:

func getData() { 
     fetchPostsWithCompletion() {
         if let tempPosts = dataFromNetwork {
             self.posts = tempPosts
         } else {
             self.posts.removeAll()
         }
         self.refresh()        // Dispatch this on the main queue if your completion handler is not already on the main queue
    }
}

这样,在获得新数据之前,您不会操纵后备阵列。

答案 1 :(得分:0)

我遇到了同样的问题。我的发现是,延迟removeAll()数组上posts的执行,可以使表格视图在其count上更新。

func getData() {
   self.delayExecutionByMilliseconds(500) {
      self.posts.removeAll()
   }
   // queries the backend and fills the data - sorry for code omission

   refresh()
}

fileprivate func delayExecutionByMilliseconds(_ delay: Int, for anonFunc: @escaping () -> Void) {
    let when = DispatchTime.now() + .milliseconds(delay)
    DispatchQueue.main.asyncAfter(deadline: when, execute: anonFunc)
}
相关问题