在uitableview单元格ios swift中加载图像时,uitableview滚动卡住了

时间:2016-03-11 06:18:21

标签: ios swift image uitableview ios9

我从api获取图片网址。我必须在我的uitableview单元格中加载这些图像。所以,当我滚动我的桌面视图时,它变得缓慢而且很难吃。

所以如何加载图像时能够快速滚动

这是我的代码:

let cell : seperatecell = tableView.dequeueReusableCellWithIdentifier("seperatecell") as! seperatecell

    let dic : NSDictionary = mylistarray.objectAtIndex(indexPath.row) as! NSDictionary
    cell.lbldate.text = dic["date"] as? String
    //  cell.lblsummary.text = dic["excerpt"] as? String
    cell.lbltitle.text = dic["title"] as? String



    //  print(dic["attachments"])

    let total = dic["attachments"]!.count

    if (total > 0)
    {
        let myarray : NSMutableArray = (dic["attachments"] as? NSMutableArray)!
        //print(myarray)
        let dic1 : NSDictionary = myarray.objectAtIndex(0) as! NSDictionary
        //print(dic1)
        var newimage : String = ""


        newimage = dic1["url"] as! String


        if let url = NSURL(string: newimage) {
            if let data = NSData(contentsOfURL: url) {
                cell.image1.image = UIImage(data: data)
            }        
        }

    }
    else
    {
        print("nil")
    }


    viewparent.hidden = false
    EZLoadingActivity.hide()

    return cell

1 个答案:

答案 0 :(得分:1)

您需要异步加载图像。

简单的方法是使用库来为您完成。你可以看看github,否则这是我使用的那个:KFSwiftImageLoader

否则,您需要在后台线程中下载图像,然后在主线程中设置图像。