我在 1 个 View Controller 中有 2 个 Collection View 。我希望第二个 Collection View 根据第一个 Collection View 中项目的选择来显示其内容。我已经完成了所有工作,可以获取要显示的数据,但我认为因为 View Controller 没有重新加载,所以它没有显示数据(单元格图像、描述等)。我如何强制刷新 View 或者是否有更好的方法或在第二个 Collection View 中显示结果?
该类的完整代码如下:
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
最佳答案
请从主线程执行此操作。
self.collectionViewPiece?.reloadData()
self.collectionViewPiece?.layoutIfNeeded();
关于ios - 从 CollectionView 的结果中显示 CollentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41297821/