Gre*_*ory 2 asynchronous uitableview uiimageview ios swift
来自以下答案的编辑代码
我想要的是
图像尺寸正确并显示在正确的位置

我得到了什么
滚动时随机出现的一堆图像

我已经查看了很多答案,但仍然没有找到解决问题的方法.对于每个人来说,这似乎经常是一个不同的问题,并且这里的大多数问题都在Objective-C中没有帮助.我发现很难从一个转换到另一个.
我将只显示我的表视图函数并解释我的代码,也许有人可以识别问题或帮助我识别自己解决它.
-------------------------------------------------- ---------------------------------
说明
并非所有新闻项目(单元格)都包含图片.所以你可以看到(在下面的第一个函数中)我遍历各个部分和嵌套的单元格,如果新闻项目包含图片if newslists[i][j] != "None,我会显示图像.从这一点来看,这是熟悉的代码,从这里的教程中可以看出
在第二个函数中,我再次循环遍历各个部分和单元格,直到我落在应该包含图像的部分上.然后我改变这个单元格的高度,使图像适合内部.
这几乎就是......任何想法?
-------------------------------------------------- ---------------------------------
的cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell
if newslists[indexPath.section][indexPath.row].imageURL != "None"{
let urlString = newslists[indexPath.section][indexPath.row].imageURL
let imgURL = NSURL(string: urlString)
cell.imageView?.image = UIImage(named: "first") // placeholder
if let img = imageCache[urlString] {
cell.imageView?.image = img
println("loaded from cache")
}
else {
let request: NSURLRequest = NSURLRequest(URL: imgURL!)
let mainQueue = NSOperationQueue.mainQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in
if error == nil {
let image = UIImage(data: data)
self.imageCache[urlString] = image
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
cellToUpdate.imageView?.image = image
println("succesfully downloaded image")
println("size of cache is \(self.imageCache.count)")
}
})
}
else {
println("Error: \(error.localizedDescription)")
}
})
}
}
cell.backgroundColor = UIColor(red: 52/255, green: 138/255, blue: 169/255, alpha: 1.0)
return cell
}
Run Code Online (Sandbox Code Playgroud)
我还有一个可能导致问题的功能,但我对此表示怀疑:
HeightForRowAtIndexPath
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if newslists[indexPath.section][indexPath.row].imageURL != "None"{
println("Expanding cell for \(indexPath.section) \(indexPath.row)")
return 190.0
}
return 70.0
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么你在这两种方法中都有一个双循环for循环.可以调用每个indexPath,这样您就可以使用indexPath部分和行获取适当的数据.
您可以尝试此代码,我认为没有任何理由不起作用.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell
cell.imageView?.image = nil
if newslists[indexPath.section][indexPath.row].imageURL != "None"{
let urlString = newslists[indexPath.section][indexPath.row].imageURL
let imgURL = NSURL(string: urlString)
cell.imageView?.image = UIImage(named: "first") // placeholder
if let img = imageCache[urlString] {
cell.imageView?.image = img
println("loaded from cache")
}
else {
...
}
}
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if newslists[indexPath.section][indexPath.row].imageURL != "None"{
println("Expanding cell for \(i) \(j)")
return 190.0
}
return 70.0
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3516 次 |
| 最近记录: |