2016-12-20 6 views
3

배열에서 데이터를받지 쿼리의 for 루프에서 모든 값을 적절한 배열로 가져옵니다. 그러나이 정보를 사용하여 내 컬렉션보기를 채울 때 아무 일도 일어나지 않습니다. 수동으로 생성 된 배열 tableImages 및 tableData에 대해 테스트를 위해 주석 처리했기 때문에이 문제를 파악할 수 없습니다.indexPath.row 내가 배열의 값을 인쇄 할 경우 배열</p> <pre><code>var packsAvailable = [""] var packsImage = [""] var packsDescription = [""] </code></pre> <p>의 몇 내 구문 분석 서버와 풋에서이 일부 문자열을 얻을 수있는 <code>PFQuery</code>을 만든

import UIKit 
import Parse 

private let reuseIdentifier = "Cell" 

class CollectionViewController: UICollectionViewController { 

//var tableData: [String] = ["Tis is a description", "Test 22", "Test 33"] 
//var tableImages: [String] = ["walk1bg", "2", "walk3bg"] 


var packsAvailable = [""] // the total packs in the app 
var packsImage = [""] 
var packsDescription = [""] 

override func viewDidLoad() { 
    super.viewDidLoad() 


    let packQuery = PFQuery(className: "Pack") 

    packQuery.findObjectsInBackground(block: { (objectsArray, error) in 
     if error != nil { 
      print(error!) 
     } else if let packs = objectsArray { 
      self.packsAvailable.removeAll() // remove them incase they double up 

      print(packs.count) 
      for object in packs { 
       /* 
       print(object["packName"]) 
       print(object["packImage"]) 
       print(object["packDesctription"]) 
       */ 
       self.packsAvailable.append(object["packName"] as! String) 
       self.packsImage.append(object["packImage"] as! String) 
       self.packsDescription.append(object["packDesctription"] as! String) 

       /* 
       print(self.packsAvailable) 
       print(self.packsImage) 
       print(self.packsDescription) 
       */ 
      } 
     } 
    }) 
} 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 


// MARK: UICollectionViewDataSource 

override func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return packsAvailable.count 
} 


func UIColorFromHEX(hexValue: UInt) -> UIColor { 
    return UIColor(
     red: CGFloat((hexValue & 0xFF0000) >> 16)/255.0, 
     green: CGFloat((hexValue & 0x00FF00) >> 8)/255.0, 
     blue: CGFloat(hexValue & 0x0000FF)/255.0, 
     alpha: CGFloat(1.0) 
    ) 
} 


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell 

    //cell.labelCell.text = tableData[indexPath.row] 
    //cell.imageCell.image = UIImage(named: tableImages[indexPath.row]) 

    cell.labelCell.text = packsDescription[indexPath.row] 

    print(packsDescription[indexPath.row]) 

    cell.imageCell.image = UIImage(named: packsImage[indexPath.row]) 

    cell.imageCell.layer.masksToBounds = true 
    cell.imageCell.layer.cornerRadius = cell.imageCell.frame.height/2 

    cell.imageCell.layer.borderWidth = 3 
    cell.imageCell.layer.borderColor = UIColorFromHEX(hexValue: 0x62aca2).cgColor 

    return cell 

} 

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    print("Selected \(indexPath.row)") 
} 

} 
+0

안녕하세요이 같은 시도 http://stackoverflow.com/questions/29812422/swift-converting-pfquery-to-string-array-for-tableview –

+0

HTTP 완료 후 collectionView.reloadData() 추가 //stackoverflow.com/questions/30227511/swift-1-2-and-parse-issue-with-retrieving-images-to-populate-pfquerycollectionv –

답변

2

for 루프가 클로저 내부에서 완료된 후 collectionView.reloadData()를 추가하십시오. 그러면 collectionView에 현재 배열 값을 가져 오도록 지시합니다.

+0

감사합니다, 내 머리를하고있었습니다. – Pippo

+0

환영합니다! 나는 감각을 알고 – JustinM

1

응답이 서버에서 오는 경우 collectionView를 다시로드하는 것을 잊었습니다. : 단지 루프

override func viewDidLoad() { 
     super.viewDidLoad() 

    let packQuery = PFQuery(className: "Pack") 

    packQuery.findObjectsInBackground(block: { (objectsArray, error) in 
     if error != nil { 
      print(error!) 
     } else if let packs = objectsArray { 
      self.packsAvailable.removeAll() // remove them incase they double up 

      print(packs.count) 
      for object in packs { 
       /* 
       print(object["packName"]) 
       print(object["packImage"]) 
       print(object["packDesctription"]) 
       */ 
       self.packsAvailable.append(object["packName"] as! String) 
       self.packsImage.append(object["packImage"] as! String) 
       self.packsDescription.append(object["packDesctription"] as! String) 

       /* 
       print(self.packsAvailable) 
       print(self.packsImage) 
       print(self.packsDescription) 
       */ 
      } 

       //swift 2.3 
       dispatch_async(dispatch_get_main_queue()) { 
        self.collectionView.reloadData() 
       } 

       //swift 3 
       /* DispatchQueue.main.async { 
       self.collectionView.reloadData() 
       }*/ 
     } 
    }) 

} 
+0

suhit, 좋은 대답은 그냥 밖으로 루프 또는 그 각 개체를 반복하는 동안 호출 얻을 얻을 reloadData 호출을 이동하십시오 – JustinM

+0

예를 들어, 루프 후 reloadData 호출을 이동 – suhit