【发布时间】:2016-04-20 11:10:03
【问题描述】:
我有一个错误:
此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃
每当我尝试将图像视图加载到表格视图中时。 相关代码如下:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ArticleCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ArticleCell
let a: Article = self.articlesArray[indexPath.row] as! Article
//let a: Article = self.newArticlesArray[indexPath.row] as! Article
cell.titleLabel.text = a.title
cell.descLabel.text = a.desc
let data = NSData(contentsOfURL: a.thumbnail)
cell.iView.image = UIImage(data: data!)
return cell
}
func downloadArticles(){
//JSON parsing methods here
self.syncCompleted = true
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
task.resume()
}
每次我在表格视图中单击图像时,图像都会移动到单元格的左侧,如果我加载的图像不是 UIImageView 的大小,那么它会全部调整大小。
我该如何解决这个错误?
我已经尝试了 dispatch_get_main_queue 代码,但它似乎对我没有多大作用。
【问题讨论】:
-
你能在你的
tableView:cellForRowAtIndexPath:中设置一个断点,然后从调用它的地方检查堆栈跟踪吗?如果它在后台线程上,您通常应该能够看到表重新加载的来源。通过这种方式,您可以发现需要分派到主队列的位置... -
@Tirat2131 当我尝试重现类似问题时,我得到了 Stack trace 以及您提到的消息。您是否在日志中获取堆栈跟踪?有的话可以提供吗?
-
我该怎么做?
标签: ios swift uitableview uiimageview