1 개의 뷰 컨트롤러 내에 2 개의 콜렉션 뷰가 있습니다. 제 2 콜렉션 뷰가 첫 콜렉션 뷰에있는 아이템의 선택에 기초하여 그 내용을 디스플레이하기를 원합니다. 필자는 모든 것을 표시 할 데이터를 얻는 지점으로 가져 왔지만 viewcontroller가 데이터 (셀 이미지, 설명 등)를 표시하지 않고 다시로드하지 않기 때문에 생각합니다. 내가 강제로보기를 강제로 새로 고칠 수 있습니까? 아니면 더 좋은 방법이 있거나 2 번째 collectionview에서 결과를 보여줄 수 있습니까? 클래스에 대한collectionview의 결과에서 collentionview를 표시합니다.
전체 코드는 다음과 같습니다 : 모든 UI 업데이트가 주 스레드에서 수행해야하기 때문에
import UIKit
import Parse
private let reuseIdentifierPart = "CellPart"
private let reuseIdentifierPiece = "CellPiece"
class JourneyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
// passed packName from PackViewController
var selectedPack: String!
var selectedPart: String!
@IBOutlet var collectionViewPart: UICollectionView!
@IBOutlet var collectionViewPiece: UICollectionView!
struct partStruct {
var partName : String
var partDescription : String
var partTitle : String
var partImage : PFFile
var partID: String
}
struct pieceStruct {
var pieceName : String
//var pieceDescription : String
//var pieceTitle : String
var pieceImage : PFFile
}
var partArray = [partStruct]()
var pieceArray = [pieceStruct]()
var refreshView: UIViewController!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// if selected pack not nil
//--------------------------------------------- PART DISPLAY IN COLLECTION ----
if let partsToDisplay = selectedPack {
let partQuery = PFQuery(className: "Part")
//have to create PFObject from selectedPack
partQuery.whereKey("fromPack", equalTo: PFObject(outDataWithClassName: "Pack", objectId: selectedPack))
partQuery.findObjectsInBackground(block: { (objectsArray, error) in
if error != nil {
print(error!)
} else if let parts = objectsArray {
for object in parts {
let arrayName = object.object(forKey: "partName") as! String
let arrayDescription = object.object(forKey: "partDescription") as! String
let arrayTitle = object.object(forKey: "partTitle") as! String
let arrayImage = object.object(forKey: "partImage") as! PFFile
//let arrayPoint = object.object(forKey: "fromPack") as! String
let arrayID = object.objectId as String!
self.partArray.append(partStruct(partName: arrayName, partDescription: arrayDescription, partTitle: arrayTitle, partImage: arrayImage, partID: arrayID!))
}
self.partArray = self.partArray.sorted{ $0.partName < $1.partName }
self.collectionViewPart?.reloadData()
}
})
}
}//viewwillappear
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.collectionViewPart {
//--------------------------------------------- PIECE DISPLAY IN COLLECTION ----
print("I selected an item in the Parts stack")
selectedPart = self.partArray[indexPath.item].partID
print("i selected item \(selectedPart)")
if let piecesToDisplay = selectedPart {
let pieceQuery = PFQuery(className: "Piece")
//have to create PFObject from selectedPack
pieceQuery.whereKey("fromPart", equalTo: PFObject(outDataWithClassName: "Part", objectId: selectedPart))
pieceQuery.findObjectsInBackground(block: { (objectsArray, error) in
if error != nil {
print(error!)
} else if let pieces = objectsArray {
print(objectsArray?.count)
for object in pieces {
let arrayName = object.object(forKey: "pieceName") as! String
let arrayImage = object.object(forKey: "pieceImage") as! PFFile
self.pieceArray.append(pieceStruct(pieceName: arrayName, pieceImage: arrayImage))
}
self.pieceArray = self.pieceArray.sorted{ $0.pieceName < $1.pieceName }
self.collectionViewPiece?.reloadData()
}
})
} else {
// this is for selecting somehitng in the vertical stack
print("WERE GOING TO GO TO THE NEXT SCREEN AND PLAY THE SOUND FILE")
}
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewPart {
//pass this to the piece query when selected
let selectedPart = self.partArray[indexPath.item].partName
//--------------------------------------------- PART HOW TO DISPLAY EACH ITEM ----
let cellPart: PartCollectionViewCell = collectionViewPart.dequeueReusableCell(withReuseIdentifier: reuseIdentifierPart, for: indexPath) as! PartCollectionViewCell
//cellPart.labelCell.text = self.packArray[indexPath.item].packDescription
//cellPart.labelCell.textColor = MyFunctions.UIColorFromHEX(hexValue: 0x0c1537)
cellPart.imageCellPart.file = self.partArray[indexPath.item].partImage
cellPart.imageCellPart.loadInBackground()
cellPart.imageCellPart.layer.masksToBounds = true
cellPart.imageCellPart.layer.cornerRadius = cellPart.imageCellPart.frame.height/2
cellPart.imageCellPart.layer.borderWidth = 3
cellPart.imageCellPart.layer.borderColor = MyFunctions.UIColorFromHEX(hexValue: 0x62aca2).cgColor
return cellPart
} else {
//--------------------------------------------- PIECE HOW TO DISPLAY EACH ITEM ----
let cellPiece: PieceCollectionViewCell = collectionViewPiece.dequeueReusableCell(withReuseIdentifier: reuseIdentifierPiece, for: indexPath) as! PieceCollectionViewCell
//cellPiece.labelCell.text = self.packArray[indexPath.item].packDescription
//cellPiece.labelCell.textColor = MyFunctions.UIColorFromHEX(hexValue: 0x0c1537)
cellPiece.imageCellPiece.file = self.pieceArray[indexPath.item].pieceImage
cellPiece.imageCellPiece.loadInBackground()
cellPiece.imageCellPiece.layer.masksToBounds = true
cellPiece.imageCellPiece.layer.cornerRadius = cellPiece.imageCellPiece.frame.height/2
cellPiece.imageCellPiece.layer.borderWidth = 3
cellPiece.imageCellPiece.layer.borderColor = MyFunctions.UIColorFromHEX(hexValue: 0x62aca2).cgColor
return cellPiece
}
}
// MARK: UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionViewPart {
return self.partArray.count
} else {
return self.pieceArray.count
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}//class
우선 대리인을 자기에게 할당하고 결과를 작성하십시오. – C0mrade