UITableViewCell 图像在选择之前不会显示

UITableViewCell image not shown until selected

我的 UITableViewCells 图像一直在显示,直到我向上滚动,因此在 selected 单元格之前图像不会显示。

当我从另一个 ViewController 切换到初始 ViewController*(包含图像)*

时,也会发生同样的问题

我检查过图片的 imgURL 是正确的。

使用的库是:图像的 AFNetworking

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("FeedCell", forIndexPath: indexPath) as! MyCell
    cell.itemImageView.image = nil
    self.configureCell(cell, atIndexPath: indexPath)
    return cell
}

// AFNetworking download and display image
func uploadIMG(cell:MyCell,imgURL:NSURL,placeholderIMG:String,atIndexPath indexPath: NSIndexPath) {

    var imageRequest: NSURLRequest = NSURLRequest(URL: imgURL)
    cell.itemImageView!.setImageWithURLRequest(imageRequest, placeholderImage: UIImage(contentsOfFile: "logo.png"), success: { [weak cell] request,response,image in

        if (cell != nil) {
            cell!.itemImageView.image = image
        }}        
        , failure: nil)
}

// called from cellForRowAtIndexPath, retrieve img url to update image
func configureCell(cell: MyCell, atIndexPath indexPath: NSIndexPath) {
    let item = self.items[indexPath.row] as MWFeedItem
    var URLofImage: NSURL = NSURL(string: item.link)!
    var session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(URLofImage, completionHandler: {(data,response, error) in
        let text = NSString(data: data, encoding: NSUTF8StringEncoding)
        var home = HTMLDocument(data: data, contentTypeHeader: text as! String)
        var div = home.nodesMatchingSelector("img")
        var urlString = div[1].firstNodeMatchingSelector("img")
        let urlData = (urlString as HTMLElement).firstNodeMatchingSelector("img")
        var urlFinal = urlData.attributes["src"]! as! String

        if urlFinal != "/images/system/bookmark-shorturl.png" {
            // call updateIMG function
            self.uploadIMG(cell, imgURL: NSURL(string: "http:www.animenewsnetwork.com" + urlFinal)!, placeholderIMG: "logo.png",atIndexPath: indexPath)
        }
    })

问题的图像表示(初始图像工作正常)

第二张图片(我向下滚动然后向上滚动,图片未显示)

我 select 一些细胞和这些细胞的图像将出现

尝试在将图像设置到单元格后,通过调用方法 tableView:reloadRowsAtIndexPaths:withRowAnimation 在 table 视图中更新该单元格。或者使用自定义图像视图编写自定义单元格。请不要忘记图像设置代码必须在主线程中运行。

误读了问题,但保留这个以防有人遇到类似问题,但使用自动布局。

我相信您正在使用自动版式。因此,如果 imageView 的帧大小使用的是固有内容大小,即图像的大小,则当没有图像时它将为 CGSizeZero。第一次显示cell时没有图片,因为需要下载。然后图像被下载并分配给 imageView.image。这不会自动使布局无效。您需要这样做,以便根据图像的大小重新计算 imageView 框架。它在滚动离开并向后滚动或选择它后出现的原因是因为当时已经下载了图像,并且在再次显示或选择时重新计算单元格布局。

下面是我的TestCell和TestViewController

import UIKit
import AFNetworking

class TestCell : UITableViewCell {
    static let cellIdentifier = "TestCell"

    @IBOutlet var downloadedImageView: UIImageView!
    @IBOutlet var rowLabel: UILabel!
    @IBOutlet var statusLabel: UILabel!

}


class TestTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableView.rowHeight = 100
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 30;
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(TestCell.cellIdentifier, forIndexPath: indexPath) as! TestCell

        let randomName = "\(Random.firstName().lowercaseString).\(Random.lastName().lowercaseString)"
        let randomImageURL = NSURL(string: Random.avatarImageURL(name: randomName))!

        cell.rowLabel.text = String(indexPath.row)
        cell.statusLabel.text = "Not Downloaded"

        var imageRequest: NSURLRequest = NSURLRequest(URL: randomImageURL)
        cell.downloadedImageView.setImageWithURLRequest(imageRequest, placeholderImage: UIImage(named: "placeholder.png"),
            success: { [weak cell]
                (request, response, image) in
                if let cell = cell {
                    cell.downloadedImageView.image = image
                    cell.rowLabel.text = String(indexPath.row)
                    cell.statusLabel.text = "Downloaded"
                }
            },
            failure: { [weak cell]
                (request, response, error) in
                if let cell = cell {
                    cell.downloadedImageView.image = nil
                    cell.rowLabel.text = String(indexPath.row)
                    cell.statusLabel.text = "Failed: \(error.localizedDescription)"
                }
            })

        return cell

    }



}

//
//  Random.swift

import Foundation


class Random {


    static let firstNames = ["Tora", "Shasta", "Camelia", "Gertrudis", "Charita", "Donita", "Debbra", "Shaquana", "Tommy", "Shara", "Ignacia", "Cassondra", "Melynda", "Lisette", "Herman", "Rhoda", "Farah", "Tim", "Tonette", "Johnathon", "Debroah", "Britni", "Charolette", "Kyoko", "Eura", "Nevada", "Lasandra", "Alpha", "Mirella", "Kristel", "Yolande", "Nelle", "Kiley", "Liberty", "Jettie", "Zoe", "Isobel", "Sheryl", "Emerita", "Hildegarde", "Launa", "Tanesha", "Pearlie", "Julianna", "Toi", "Terina", "Collin", "Shamika", "Suzette", "Tad"]

    static let lastNames = ["Austen", "Kenton", "Blomker", "Demars", "Bibbs", "Eoff", "Alcantara", "Swade", "Klinefelter", "Riese", "Smades", "Fryson", "Altobelli", "Deleeuw", "Beckner", "Valone", "Tarbox", "Shumate", "Tabone", "Kellam", "Dibiase", "Fasick", "Curington", "Holbrook", "Sulzer", "Bearden", "Siren", "Kennedy", "Dulak", "Segers", "Roark", "Mauck", "Horsman", "Montreuil", "Leyva", "Veltz", "Roldan", "Denlinger", "James", "Oriley", "Cistrunk", "Rhodes", "Mcginness", "Gallop", "Constantine", "Niece", "Sabine", "Vegter", "Sarnicola", "Towler"]


    class func int(#min: Int, max: Int) -> Int {
        return Int(arc4random_uniform(UInt32(max-min))) + min //???: RTFM on arc4random, might be need (max+1)-min.
    }

    class func int(#range: Range<Int>) -> Int {
        return int(min: range.startIndex, max: range.endIndex)
    }

    class func selectElement<T>(#array: [T]) -> T {
        return array[int(range: 0..<array.count)]
    }


    class func firstName() -> String {
        return Random.selectElement(array: Random.firstNames)
    }

    class func lastName() -> String {
        return Random.selectElement(array: Random.lastNames)
    }

    class func avatarImageURL(var name: String? = nil) -> String {

        if name == nil {
            name = "(Random.firstName().lowercaseString).Random.lastName().lowercaseString"
        }

        let avatarImageSize = Random.int(min: 40, max: 285)
        return "http://api.adorable.io/avatars/\(avatarImageSize)/\(name!)@gmail.png"
    }


    class func imageURL() -> String {

        let imageWidth = Random.int(min:120, max:1080)
        let imageHeight = Random.int(min:120, max:1080)

        return "http://lorempixel.com/g/\(imageWidth)/\(imageHeight)/"
    }

}

滚动时,单元格将重新加载。 (您重新加载以重新下载您的图片)-> 有问题。

已解决: 下载后创建数组保存图像数据。

单元格从这个数组中获取图像,而不是重新下载

希望对您有所帮助!

问题是我的图像没有在主线程上设置。为了解决这个问题,我简单地使用了下面的代码,确保我的图像会立即设置。

dispatch_async(dispatch_get_main_queue(), {
 // do image functions here

)}