我目前有一个Viewcontroller,它可以解决大量数据的问题。然后我需要将此数据推送到UIContainerView内的UICollectionView。我试过通过segue推送,但我需要不断刷新数据,所以我不断收到错误:
'There are unexpected subviews in the container view. Perhaps the embed segue has already fired once or a subview was added programmatically?'
然后我继续调查代理并制定了一个协议来共享两个类之间的数据,但是如何从我的初始视图控制器启动reloadData函数呢?
我对代表们来说还是新手,但我已经设置了以下内容:
protocol ProductsViewControllerDelegate {
var catalogue : CatalogueData {get set}
}
然后我让我的第一个视图控制器继承此委托并将数据传递给它。然后我在集合视图中创建了一个委托实例,在segue我设置了collectionview.delegate = self
但是当我向其传递新数据时如何刷新数据?
更新
所以我认为我的问题是数据首先没有通过。我的设置如下:
查看控制器:
struct CatalogueData {
var data : NSArray
var numberOfRows : Int
var numberOfColumns : Int
var tileName : XibNames
}
class ProductsViewController:UIViewController, CollectionCatalogueDelegate{
internal var catalogue: CatalogueData!
override func viewDidLoad() {
super.viewDidLoad()
let catdta : CatalogueData = CatalogueData(data: ProductCodes,
numberOfRows: 2,
numberOfColumns: 4,
tileName: XibNames.ProductDisplayTall)
self.catalogue = catdta
}
}
容器视图内的第二个集合视图的设置如下:
protocol CollectionCatalogueDelegate {
var catalogue : CatalogueData! {get set}
}
class CollectionCatalogue: UICollectionViewController, UICollectionViewDelegateFlowLayout{
@IBOutlet var cv: UICollectionView?
var delegate : CollectionCatalogueDelegate?{
didSet{
cv?.reloadData()
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (self.delegate?.catalogue.data.count)!
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
self.cv?.register(UINib(nibName: (self.delegate?.catalogue?.tileName.rawValue)!, bundle: nil), forCellWithReuseIdentifier: (self.delegate?.catalogue?.tileName.rawValue)!)
let c:ProductCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: selectedXib.rawValue, for: indexPath) as! ProductCollectionCell
c.layer.borderColor = UIColor.black.cgColor
c.layer.borderWidth = 0.5
c.layer.cornerRadius = 3
let mng = (self.delegate?.catalogue?.data)![indexPath.row] as? NSManagedObject
'...........DO STUFF WITH DATA HERE ETC
}
}
依赖于其他操作,我将在第一个视图控制器中更新“目录”,并在集合视图中显示结果。