alamofire请求完成后更新tableview

时间:2015-12-04 07:17:37

标签: ios swift2 alamofire

我从我的服务器获取了一个json块,然后我继续处理我的托管对象,完成后我的表视图被重新加载以显示新内容,这除了一个小问题之外还可以。

在json的处理过程中,我获得了一个图像的URL,我在UIImageView上使用了一个扩展来再次使用alamofire,并将下载的图像写入磁盘。

问题是图像应该显示在其托管对象的同一个表行上,但是发生的情况是整个json的处理完成并在保存到磁盘完成之前返回到tableview,因此图像不会来了。

如何让tableview知道每个图像在完成保存时已准备就绪,以便其相关行可以更新?

扩展名UIImageView {

//
// downloads image from supplied url and writes to disk using supplied destination path
//
func imageFromUrl(urlString: String, destinationPath: String) {

    Alamofire.request(.GET, urlString).response { _, _, data, _ in

        // only if we go data
        if let data = data {

            // cast to jpg image
            let imageData = NSData(data: UIImageJPEGRepresentation(UIImage(data: data)!, 1)!)

            // and write to disk
            imageData.writeToFile(destinationPath, atomically: true)
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

尝试:

func imageFromUrl(urlString: String, destinationPath: String,handleFinishLoad:()->()) {

    Alamofire.request(.GET, urlString).response { _, _, data, _ in

        // only if we go data
        if let data = data {

            // cast to jpg image
            let imageData = NSData(data: UIImageJPEGRepresentation(UIImage(data: data)!, 1)!)

            // and write to disk
            imageData.writeToFile(destinationPath, atomically: true)
            handleFinishLoad()
        }
    }
}

并在tableviewcell中:

    yourImageView.imageFromUrl(url, destinationPath: path) { () -> () in
        // downloadfinish 
        // update cell ...
    }

更新:

//    in class A: DataService
//    i use static function to example. You should create a singleton for DataService and use func : singleton.getData....
    static func getData(handleComplete:(datas:NSArray)->()){ // get json data from server
        // data is array with every item is your object with contain urlimage, name... or it 's nsdictionary too
        Alamofire.request(.GET, urlString).response { _, _, data, _ in
            let arrayReturn = NSMutableArray()
            // parse data and add to a array or add every dictionary to array
            handleComplete(arrayReturn)
        }
    }

//    in class B:  yourViewController (this class contain your tableview)
//    in viewdidload
    override func viewDidLoad() {
        super.viewDidLoad()
        self.getData { (datas) -> () in
            // set data for tableview
            self.tableviewData = datas
            // numberofrow tableview is self.tableviewData.count
            // every cell of tableview have data is every item of self.tableviewData
            self.tableview.reloadData()
        }
    }

//    in  CellForRowAtIndexpath, create your cell- have uiimageview
//     and setimage for it
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // create cell
        cell.imageview.imageFromUrl(url, destinationPath: path) { () -> () in
            // downloadfinish
            // set image for cell.imageview
            // But it not good. You can use SDWebImage lib , set image 's url for uiimageview. it 's so easy
        }
        return cell
    }