gpt4 book ai didi

ios - 在 ViewWillAppear 完成运行之前调用 CellForRowAtIndexPath

转载 作者:可可西里 更新时间:2023-11-01 02:18:01 26 4
gpt4 key购买 nike

我有一个从 Parse 数据库中提取信息并将其显示在 UITableView 中的应用程序。我从 viewWillAppear 函数中的解析中提取信息,并将其显示在 tableView(cellForRowAtIndexPath) 函数中。有时我收到一个错误,因为存储 Parse 信息的数组的长度为 0,并且我尝试访问数组边界之外的索引处的信息。我相信这是因为在 viewWillAppear 完成运行之前调用了 cellForRowAtIndexPath。这是可能的还是我的错误肯定来自其他地方?

编辑:错误不会每次都发生,我找不到重现它的方法

override func viewWillAppear(animated: Bool) {

//begin ignoring events until the information is finished being pulled
UIApplication.sharedApplication().beginIgnoringInteractionEvents()

resultsArray.removeAll(keepCapacity: false)

//run query
let query = PFQuery(className: "Answers")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if let objects = objects {

//append information to the resultsArray
}
}
self.tableView.reloadData()
}
}
//information is now pulled, so allow interaction
UIApplication.sharedApplication().endIgnoringInteractionEvents()

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! answerCell

// THIS IS WHERE THE ERROR OCCURS
resultsArray[indexPath.row].imageFile.getDataInBackgroundWithBlock { (data, error) -> Void in

//set image within cell
}
return cell
}

最佳答案

我建议您将数据从 Parse 加载到一个临时数组中,然后在您调用 reloadData 之前将其分配给您的属性数组 - 这将避免任何潜在的竞争条件并消除对removeAll 这可能是您问题的重要组成部分;

override func viewWillAppear(animated: Bool) {

//begin ignoring events until the information is finished being pulled
UIApplication.sharedApplication().beginIgnoringInteractionEvents()



//run query
let query = PFQuery(className: "Answers")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

var localArray=[SomeType]()

if let objects = objects {

//append information to the localArray
}
}
self.resultsArray=localArray
self.tableView.reloadData()
}
}
//information is now pulled, so allow interaction
UIApplication.sharedApplication().endIgnoringInteractionEvents()

}

关于ios - 在 ViewWillAppear 完成运行之前调用 CellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34973238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com