在表格视图单元格中显示 Activity 指示符

Display Activity Indicator in tableview cell

我正在基本的表格视图单元格中加载不同的图像,并希望在加载图像时在单元格中显示 activity 指示符。我正在使用一个单独的单元格文件,例如示例代码。我知道在哪里放置代码只是不确定语法。我找到了几种将代码放入视图但不是专门放在单元格中的方法。有人有想法吗?谢谢

func configureCell(favorite: Article) {
    self.favorite = favorite

    downloadImage(NSURL(string: self.favorite.mainImage)!)

func downloadImage(url: NSURL, type: String) {
    getDataFromUrl(url) { (data, response, error) in
        //show activity indicator
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            guard let data = data where error == nil else { return }
                self.mainImage.image = UIImage(data: data)
        }
    }
}

func getDataFromUrl(url: NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError?) -> Void)) {

    NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
        completion(data: data, response: response, error: error)
    }.resume()

}

你可以像下面那样做,如果这对你有用,请告诉我,你可以根据你的要求修改 frame

func downloadImage(url: NSURL, type: String) {
    let activityIndicator = UIActivityIndicatorView()
    activityIndicator.frame = self.mainImage.bounds

    getDataFromUrl(url) { (data, response, error) in
        //show activity indicator
        self.mainImage.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            guard let data = data where error == nil else { return }
            activityIndicator.stopAnimating()
            activityIndicator.removeFromSuperview()
            self.mainImage.image = UIImage(data: data)
        }
    }
}