1
我在基本tableview單元格中加載不同的圖像,並希望在加載圖像時顯示單元格中的活動指示器。我正在使用單獨的單元格文件,如示例代碼。我知道在哪裏放置代碼只是不確定語法。我發現了幾種將代碼放入視圖中的方法,但不是特別在單元中。任何人有想法?謝謝。tableview單元格中的顯示活動指示器
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()
}
感謝您的快速回復,這是工作!它只是沒有定位在中心位置,在右邊,但它顯示出來。我還必須在getDataFromUrl函數上面移動self.mainImage.addSubview(activityIndicator)和activityIndicator.startAnimating()。 – user3712837
我似乎無法正確居中你知道嗎?我試過這個,但它不是很正確:activityIndicator.frame = CGRect(x:0,y:0,width:self.mainImage.bounds.width/2,height:self.mainImage.bounds.height/2) – user3712837
你的x,y位置不正確,你需要把它放在超級視圖 – Santosh