TableView Cell show image with dispatch_async shows “unexpected non-void return value in void function” issue

谁都会走 提交于 2019-12-25 20:57:20

问题


I want to show the image in the TableViewCell. There are the codes:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "myCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DIYSquareALLCell
        cell.titles!.text = titles[indexPath.row]
        cell.leftImages!.image = getPic(leftImages[indexPath.row])
        return cell
    }

func getPic(PicURL: String) -> UIImage! {
        let image = self.imageCache[PicURL] as UIImage?
        if image == nil {
            let url = NSURL(string: PicURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
            if let data = NSData(contentsOfURL: url!) {
                imageCache[PicURL] = UIImage(data: data)
                return UIImage(data: data)!
            }
        } else {
            return image
        }
        return nil
    }

But scrolling the TableView is very lag so I change the function and add some dispatch_async feature in it.

It shows the issue "unexpected non-void return value in void function" in my getPic function.

After I changed, there are the codes:

func getPic(PicURL: String) -> UIImage! {
        let image = self.imageCache[PicURL] as UIImage?
        if image == nil {
            let url = NSURL(string: PicURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
            let priority = DISPATCH_QUEUE_PRIORITY_HIGH
            dispatch_async(dispatch_get_global_queue(priority, 0)) {
                let data = NSData(contentsOfURL: url!)
                if data != nil {
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.imageCache[PicURL] = UIImage(data: data!)
                        return UIImage(data: data!)// here is the issue
                    })
                }
            }
        } else {
            return image
        }
        return nil
    }

Anyone can tell me how to fix it? Thanks!


回答1:


You can't return a value r an object when using the Asynchronous task, The function which is running on the main thread and it won't wait for your async task to be finish.

Lets do this with the Closure.

Your code should be like this:

 typealias CompletionHandler = (image: UIImage) -> Void
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: testCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! testCell
        downloadFileFromURL(NSURL(string: "http://img.youtube.com/vi/PCwL3-hkKrg/sddefault.jpg")!, completionHandler:{(img) in
             dispatch_async(dispatch_get_main_queue(), { () -> Void in
                cell.imgView.image = img
             })

            })
        return cell
    }

    func downloadFileFromURL(url1: NSURL?,completionHandler: CompletionHandler) {
        // download code.
        if let url = url1{
        let priority = DISPATCH_QUEUE_PRIORITY_HIGH
        dispatch_async(dispatch_get_global_queue(priority, 0)) {
            let data = NSData(contentsOfURL: url)
            if data != nil {
                print("image downloaded")
                completionHandler(image: UIImage(data: data!)!)
            }
            }
        }
    }

Sample project uploaded to GIT.



来源:https://stackoverflow.com/questions/37540785/tableview-cell-show-image-with-dispatch-async-shows-unexpected-non-void-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!