【发布时间】:2016-10-13 05:41:05
【问题描述】:
由于某种原因,我的代码只显示 JSON 文件中的最后一个条目...没有错误,只是没有正确显示 UICollectionView 中的所有集合。
class vcWatch: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var myCollectionView: UICollectionView!
var images:[String] = [] // @IBOutlet weak var imageCell: UIImageView!
var lableTitles:[String] = [] // @IBOutlet weak var labelCell: UILabel!
let pageURL = "http://alifetouched.com/lib/videos.json.php"
override func viewDidLoad() {
super.viewDidLoad()
loadImages()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let myCell:cvCell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! cvCell
myCell.labelCell.text = self.lableTitles[indexPath.row]
let imageString = self.images[indexPath.row]
let imageUrl = NSURL(string: imageString)
let imageData = NSData(contentsOf: imageUrl! as URL)
if(imageData != nil){
myCell.imageCell.image = UIImage(data: imageData! as Data)
}
return myCell
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath){
print("User Tapped: \(indexPath.row)")
}
func loadImages() {
myActivityIndicator.isHidden = false
myActivityIndicator.startAnimating()
Alamofire.request(pageURL)
.validate()
.responseJSON { (response) in
guard response.result.isSuccess else {
print("Error with response: \(response.result.error)")
return
}
guard let dict = response.result.value as? Dictionary <String,AnyObject> else {
print("Error with dictionary: \(response.result.error)")
return
}
guard let dictData = dict["VideoJSON"] as? [Dictionary <String,AnyObject>] else {
print("Error with dictionary data: \(response.result.error)")
return
}
for videos in dictData {
self.myActivityIndicator.isHidden = true
self.myActivityIndicator.stopAnimating()
self.images = [videos["icon_url"] as! String]
self.lableTitles = [videos["title"] as! String]
self.myCollectionView.reloadData()
}
return
}
}
}
我知道我只是想不通...提前感谢您的宝贵时间。
【问题讨论】:
标签: json swift uicollectionview swift3 alamofire