Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell图像在选中之前不显示_Ios_Objective C_Uitableview_Swift_Afnetworking - Fatal编程技术网

Ios UITableViewCell图像在选中之前不显示

Ios UITableViewCell图像在选中之前不显示,ios,objective-c,uitableview,swift,afnetworking,Ios,Objective C,Uitableview,Swift,Afnetworking,我的UITableViewCells图像将一直显示,直到我向上滚动,这样,在选择单元格之前,图像将不会显示 当我从另一个ViewController切换到初始ViewController*(包含图像)时,同样的问题也会发生* 我已检查图像的图像是否正确 所使用的库是:用于映像的AFNetworking override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->

我的UITableViewCells图像将一直显示,直到我向上滚动,这样,在选择单元格之前,图像将不会显示

当我从另一个ViewController切换到初始ViewController*(包含图像)时,同样的问题也会发生*

我已检查图像的图像是否正确

所使用的库是:用于映像的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)
        }
    })
问题的图像表示(初始图像工作正常)

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

我选择一些单元格,然后这些单元格的图像就会出现


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

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

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

我相信您正在使用自动布局。因此,如果imageView的帧大小使用固有的内容大小,即它的图像大小,那么当没有图像时,它将是CGSizeZero。单元格首次显示时没有图像,因为它需要下载。然后下载图像并将其分配给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)/"
    }

}
导入UIKit
导入AFN网络
类TestCell:UITableViewCell{
静态let-cellIdentifier=“TestCell”
@IBOutlet var下载edimageview:UIImageView!
@IBVAR rowLabel:UILabel!
@IBOutlet var状态标签:UILabel!
}
类TestTableViewController:UITableViewController{
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
tableView.rowHeight=100
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回30;
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
将cell=tableView.dequeueReusableCellWithIdentifier(TestCell.cellIdentifier,forIndexPath:indexPath)设为!TestCell
让randomName=“\(Random.firstName().lowercasesetring)。\(Random.lastName().lowercasesetring)”
让randomImageURL=NSURL(字符串:Random.avatarImageURL(名称:randomName))!
cell.rowLabel.text=字符串(indexPath.row)
cell.statusLabel.text=“未下载”
var-imageRequest:NSURLRequest=NSURLRequest(URL:randomImageURL)
cell.downloadeImageView.setImageWithURLRequest(imageRequest,占位符图像:UIImage(名为:“placeholder.png”),
成功:{[弱细胞]
(请求、响应、图像)在
如果let cell=cell{
cell.downloadeImageView.image=图像
cell.rowLabel.text=字符串(indexPath.row)
cell.statusLabel.text=“已下载”
}
},
失败:{[弱单元]
(请求、响应、错误)
如果let cell=cell{
cell.downloadeImage.image=nil
cell.rowLabel.text=字符串(indexPath.row)
cell.statusLabel.text=“失败:\(错误。本地化描述)”
}
})
返回单元
}
}
//
//随机的,斯威夫特
进口基金会
类随机{
静态名字=[“托拉”、“沙斯塔”、“卡米利亚”、“格特鲁迪斯”、“查丽塔”、“多妮塔”、“黛布拉”、“沙奎纳”、“汤米”、“莎拉”、“伊格纳西亚”、“卡桑德拉”、“梅琳达”、“莉赛特”、“赫尔曼”、“罗达”、“法拉”、“提姆”、“托内特”、“约翰纳森”、“德布罗亚”、“布里蒂尼”、“夏洛莱特”、“京子”、“尤拉”、“内华达”、“拉桑德拉”、“阿尔法”、“米雷拉”、“克里斯特尔”、“约兰德”,“内尔”、“凯利”、“自由”、“杰蒂”、“佐伊”、“伊莎贝尔”、“谢丽尔”、“埃梅丽塔”、“希尔德加德”、“劳娜”、“塔妮莎”、“佩莉”、“朱丽安娜”、“托伊”、“泰丽娜”、“柯林”、“沙米卡”、“苏赛特”、“泰德”]
静态let lastNames=[“奥斯汀”、“肯顿”、“布洛姆克”、“德玛斯”、“比布斯”、“伊奥夫”、“阿尔坎塔拉”、“斯瓦德”、“克林费尔特”、“里斯”、“斯马德斯”、“弗莱森”、“阿尔托贝利”、“德莱乌”、“贝克纳”、“瓦隆”、“塔博克斯”、“舒马特”、“塔伯恩”、“凯勒姆”、“迪比亚斯”、“法西克”、“库灵顿”、“霍尔布鲁克”、“苏尔寿”、“比尔登”、“塞伦”、“肯尼迪”、“杜拉克”、“塞格斯”、“罗克””莫克、霍斯曼、蒙特勒伊、莱瓦、维尔茨、罗尔丹、丹林格、詹姆斯、奥利、西斯特朗、罗德、麦金尼斯、盖洛普、君士坦丁、侄女、萨宾、维格特、萨尼科拉、托勒]
类func int(#min:int,max:int)->int{
返回Int(arc4random_uniform(UInt32(max-min))+min/?:arc4random上的RTFM,可能需要(max+1)-min。
}
类func int(#range:range)->int{
返回整数(最小值:range.startIndex,最大值:range.endIndex)
}
类func selectElement(#数组:[T])->T{
返回数组[int(范围:0..String{
返回Random.selectElement(数组:Random.firstNames)
}
类func lastName()->String{
R
dispatch_async(dispatch_get_main_queue(), {
 // do image functions here

)}