由于某种原因,我的代码仅显示JSON文件中的最后一项...没有错误,只是没有正确显示UICollectionView
中的所有设置。Swift 3,Alamofire 4,JSON词典:仅显示上一个JSON条目
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
}
}
}
我知道我只是不想直...先谢谢你的时间。
WOW!快速而准确......只是随更新而运行它,它工作得很好。非常感谢您的时间 - 完美答案。我点击评分,但我相信我不到15分就可以得分。请评分Nirav D - 他的解决方案是现货。 – Dortoh
@ user3144951欢迎队友:)另请查看编辑后的答案:) –
今晚我回家时我会试试self.dicarr。不幸的是,我之前没有正确地设置好字典,但是我面临着挑战。我需要在cellforitemat函数或加载函数中设置它?对不起,我真的很想理解这一点,所以我可以在将来使用这种理解并付诸实施。谢谢。 – Dortoh