【发布时间】:2015-04-26 14:46:38
【问题描述】:
我在一个视图中有两个容器。顶部有一个集合视图。 当从下面的容器中点击一个按钮时,我想从一个按钮更新我的集合视图。我的按钮还更改了我的集合视图使用的数组的值。
我认为 didSet 可以完成这项工作,但不幸的是没有工作。
顶部:
class TopViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var favoritesCV: UICollectionView!
var myFavorites = [] {
didSet {
self.favoritesCV.reloadData()
}
}
override func viewDidAppear(animated: Bool) {
myFavorites = favoritesInstance.favoritesArray
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return myFavorites.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : FavoritesCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FavoritesCollectionViewCell
var myPath = myFavorites[indexPath.row] as! String
cell.myImage.image = UIImage(named: myPath)
return cell
}
}
底部:
class BottomViewController: UIViewController, UIScrollViewDelegate {
@IBAction func addFavorites(sender: AnyObject) {
favoritesInstance.favoritesArray.append("aaaa.jpg")
}
}
存储类:
class Favorites {
var favoritesArray:Array <AnyObject>
init(favoritesArray:Array <AnyObject>) {
self.favoritesArray = favoritesArray
}
}
var favoritesInstance = Favorites(favoritesArray:[])
【问题讨论】:
标签: ios swift view collections