从 collectionview 的结果中显示 collentionview
Posted
技术标签:
【中文标题】从 collectionview 的结果中显示 collentionview【英文标题】:display collentionview from result of collectionview 【发布时间】:2016-12-23 08:29:44 【问题描述】:我在 1 个视图控制器中有 2 个集合视图。我希望第二个集合视图根据第一个集合视图中的项目选择显示其内容。我已经把它全部工作到了它可以显示数据的地步,但我认为因为 viewcontroller 没有重新加载它没有显示数据(单元格图像、描述等)。我如何强制刷新视图或者有更好的方法或在第二个集合视图中显示结果?
该类的完整代码如下:
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
【问题讨论】:
首先将delegate分配给self,并写入结果。 【参考方案1】:请从你的主线程执行。
self.collectionViewPiece?.reloadData()
self.collectionViewPiece?.layoutIfNeeded();
【讨论】:
很遗憾没有产生不同的结果 (1) 我假设您已启用“启用用户交互”。 (2) 另外,您是否看到排序后的元素(调试/日志)?self.partArray = self.partArray.sorted $0.partName < $1.partName
是的,交互已启用,两个数组都正确显示
好的。在您的 cellForItemAt
实现中,我假设 imageCellPart 和 imageCellPiece 在您为其设置值时不为空。另外,我假设您有正确的约束,在情节提要上的imageCellPart & imageCellPiece
(我想是 UIImageViews)上设置显示值,并在这些单元格上映射特定的自定义类名。
是的,我已经返回并重新创建了所有的网点和指向课程的链接。但是显示有问题。它甚至没有在 collectionViewPiece 中显示占位符图像。可能是因为它是 pffileview 而不是 uiimageview【参考方案2】:
self.collectionViewPiece?.reloadData()
应该发生在主线程上,因为所有的 UI 更新都必须在主线程上完成。这样做
DispatchQueue.main.async(execute:
self.collectionViewPiece?.reloadData()
)
在didSelectItemAt
中包含上述代码,而不仅仅是self.collectionViewPiece?.reloadData()
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
DispatchQueue.main.async(execute:
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")
【讨论】:
我对此有点菜鸟,所以不清楚你所说的主线程是什么意思?这在班级内部不起作用,并且不允许在班级上方 不幸的是同样的结果。我将通过代码返回。【参考方案3】:我不知道为什么,考虑到我已经在情节提要中设置了数据源和委托,但这解决了问题。
override func viewDidLoad()
super.viewDidLoad()
collectionViewPart.delegate = self
collectionViewPiece.delegate = self
collectionViewPart.dataSource = self
collectionViewPiece.dataSource = self
【讨论】:
以上是关于从 collectionview 的结果中显示 collentionview的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Forms 如何将数据从 CollectionView 传输到不同的视图?
获取 TableviewCells 以显示从 firebase 提取的不同数据并在 CollectionView 中显示它们
如何在 CollectionView 中显示从照片库或相机中选择的图像?画廊视图
如何将图像从 CollectionView 传输/显示到另一个 ViewController