我不明白为什么我的数组超出了界限。我把源项目到一个数组“imageArray”,它给了我致命的错误继承人代码指数越界swift
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BlogPost
let blogPost: BlogPost = blogPosts[indexPath.row]
cell.textLabel?.text = blogPost.postTitle
// cell.textLabel?.text = blogPost.postImageLink
if cell.postImage?.image == nil {
// let cache = ImageLoadingWithCache()
// cache.getImage(self.postImageLink, imageView: cell.postImage, defaultImage: "IMG_0079")}
if cell.postImage?.image == nil {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// retrieve image from url
let image = UIImage(data: NSData(contentsOfURL: NSURL(string:self.postImageLink)!)!)
self.imageArray.append(image!)
dispatch_async(dispatch_get_main_queue(), { Void in
// set image in main thread
cell.postImage?.image = self.imageArray[indexPath.row]
})
}
} else {
cell.postImage?.image = UIImage(named: "IMG_0079")
}
}
return cell
}
具体的错误是在这里
cell.postImage?.image = self.imageArray[indexPath.row]
什么想法?
尝试使用此扩展名UIImageView http://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift/27712427#27712427 –
我会猜测这是因为'indexPath'是当闭包被创建时被捕获,然后在闭包被调用时'indexPath.row'变得陈旧和越界。更不用说你在那里强制展开了很多东西,如果其中一些'Optional'没有设置呢?测试并正确拆包它们! – ColGraff