[英]Repeating images when scrolling in UITableView 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
}
我還有一個可能會導致問題的功能,但我對此表示懷疑:
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
}
我不確定為什么你在這兩種方法中都有一個雙循環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
}
我們需要做的就是使用prepareForReuse()
函數。 正如這篇中篇文章中所討論的,在單元重用之前調用此函數,讓您取消當前請求並執行重置。
override func prepareForReuse() {
super.prepareForReuse()
imageView.image = nil
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.