【发布时间】:2015-10-28 22:35:17
【问题描述】:
我有问题。我想从 Internet 下载图像,并在下载时显示“指示器”。问题是当没有互联网连接时,方法“downloadedFrom”进入“if error!= nil”,因此“指示器”永远不会中断。这是我的代码。
var imageArray:[String]! = ["http://findicons.com/files/icons/1072/face_avatars/300/a01.png", "http://findicons.com/files/icons/1072/face_avatars/300/a02.png", "http://findicons.com/files/icons/1072/face_avatars/300/a03.png", "http://findicons.com/files/icons/1072/face_avatars/300/a04.png", "http://findicons.com/files/icons/1072/face_avatars/300/a05.png"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let newCell = tableView.dequeueReusableCellWithIdentifier("userCell") as! UserCell_TableViewCell
let selectedUser = userArray[indexPath.row]
newCell.userDescription?.text = selectedUser.getFirstName() + " " + selectedUser.getLastName()
let randomNumber = arc4random_uniform(4)
let arrayIndex = Int(randomNumber)
let urlImage = imageArray[arrayIndex]
newCell.loadIndicator.startAnimating()
downloadedFrom(urlImage) { userImage in
dispatch_async(dispatch_get_main_queue()){
newCell.userImage.image = userImage
newCell.loadIndicator.stopAnimating()
}
self.userArray[indexPath.row].setImage(userImage)
}
return newCell
}
func downloadedFrom(urlLink :String, completionHandler: (UIImage) -> ())
{
if let urlData = NSURL(string: urlLink) {
NSURLSession.sharedSession().dataTaskWithURL(urlData) { (data, response, error) in
if error != nil {
print("error=\(error)")
return
} else {
if let httpResponse = response as? NSHTTPURLResponse {
let userImage = httpResponse.statusCode == 200 ? UIImage(data: data!) : UIImage(named: "unknownImage")
completionHandler(userImage!)
}}
}.resume()
}
}
编辑
import UIKit
class UserCell_TableViewCell: UITableViewCell {
@IBOutlet weak var userImage: UIImageView!
@IBOutlet weak var userDescription: UILabel!
@IBOutlet weak var loadIndicator: UIActivityIndicatorView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func downloadedFrom(urlLink :String)
{
self.loadIndicator.startAnimating()
if let urlData = NSURL(string: urlLink) {
NSURLSession.sharedSession().dataTaskWithURL(urlData) { (data, response, error) in
if error != nil {
print("error = \(error)")
self.loadIndicator.stopAnimating()
return
} else {
// El servidor contesta la petición, pero la imágen puede no existir.
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
dispatch_async(dispatch_get_main_queue()){
self.userImage.image = UIImage(data: data!)
self.loadIndicator.stopAnimating()
}}
}}
}.resume()
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let newCell = tableView.dequeueReusableCellWithIdentifier("userCell") as! UserCell_TableViewCell
let selectedUser = userArray[indexPath.row]
newCell.userDescription?.text = selectedUser.getFirstName() + " " + selectedUser.getLastName()
let randomNumber = arc4random_uniform(4)
let arrayIndex = Int(randomNumber)
let urlImage = imageArray[arrayIndex]
newCell.downloadedFrom(urlImage)
return newCell
}
导致我这样做的问题是,我需要您下载图像以将其保存到数组中,方法 cellForRowAtIndexPath 中的数组完整性
【问题讨论】:
标签: ios swift nsurlsession