어떤 이유로 든 내 코드는 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
}
}
}
나는 곧장 생각하지 않는다는 것을 알고 있습니다 ... 미리 감사드립니다.
와우! 빠르고 정확합니다 ... 업데이트를 실행하고 잘 작동합니다. 시간 내 주셔서 대단히 감사합니다 - 완벽한 답변. 등급을 클릭했지만 점수가 15 점 미만이라고 생각합니다. Nirav D 점수를주세요 - 그의 해결책이 자리하고 있습니다. – Dortoh
@ user3144951 환영 메이트 :) 편집 된 답변 확인 : –
오늘 밤 집에 돌아올 때 self.dicarr를 사용해 보겠습니다. 불행히도 사전에 제대로 사전을 만들지는 못했지만 도전해야합니다. cellforitemat 함수 또는로드 함수에서이를 설정해야합니까? 미안, 나는 이것을 이해하고 싶다. 그래서 나는 미래에이 이해를 사용하고 앞으로 돈을 지불 할 수있다. 고맙습니다. – Dortoh