简体   繁体   English

单元未添加到集合视图

[英]Cell not being added to collection view

I'm trying to add a collection view cell as and when the user chooses to do so. 当用户选择添加集合视图单元格时,我试图这样做。 he does this in a separate view controller where he gives the name and saves it. 他在单独的视图控制器中执行此操作,并在其中给出名称并保存。 but when it returns back to the initial view controller the new cell doesn't get added to the collection view. 但是当它返回到初始视图控制器时,不会将新单元格添加到集合视图中。

Here is my code for the 2 view controllers : 这是我的2个视图控制器的代码:

import UIKit

class FlashViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet var collectionView: UICollectionView!

var decks: [Deck] = []

    override func viewDidLoad() {
    super.viewDidLoad()
    // Move on ...
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 75, left: 20, bottom: 10, right: 20)
    layout.itemSize = CGSize(width: 150, height: 200)
    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    collectionView.registerClass(DeckCollectionViewCell.self, forCellWithReuseIdentifier: "DeckCollectionViewCell")
    collectionView.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(collectionView!)

    var deck1 = Deck()
    deck1.name = "SAT is the bomb"
    self.decks.append(deck1)

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.decks.count
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("DeckCollectionViewCell", forIndexPath: indexPath) as DeckCollectionViewCell
    cell.backgroundColor = UIColor.blackColor()
    cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
    cell.imageView?.image = UIImage(named: "circle")
    return cell
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var nextViewController = segue.destinationViewController as NewDeckViewController
    nextViewController.deckCollection = self
}
}

Next view controller : 下一视图控制器:

import UIKit

class NewDeckViewController : UIViewController
{
@IBOutlet weak var deckNameTextField: UITextField!

var deckCollection = FlashViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    // Move on...
}

@IBAction func cancelTapped(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func createTapped(sender: AnyObject) {
    var newDeck = Deck()
    newDeck.name = self.deckNameTextField.text
    self.deckCollection.decks.append(newDeck)
    self.dismissViewControllerAnimated(true, completion: nil)
}


}

You need to reload the collection view 您需要重新加载收藏夹视图

self.collectionView.reloadData

Or alternatively you can use reloadItemsAtIndexPath if you don't want to reload the entire collection view / want to show an animation, assuming you know the indexPath of the new item. 或者,如果您不想重新加载整个集合视图/不想显示动画,则可以使用reloadItemsAtIndexPath ,假设您知道新项目的indexPath。

Now the question is, when do you call this? 现在的问题是,您何时称呼它?

Easiest thing for your current implementation is in viewWillAppear , but that will also reload you collection view when it does not need to be reloaded (eg. the first time the view appears, or if the next view controller returns without appending anything etc -- not a huge deal if your collection view is small, but it can easily be done better). 当前实现最简单的方法是在viewWillAppear ,但是当不需要重新加载集合视图时(例如,第一次出现该视图,或者如果下一个视图控制器返回而未追加任何内容,则该视图也将重新加载)-否如果您的收藏夹视图很小,那将是一笔大数目,但可以很容易地做得更好)。 You could also call it from the second view controller right after the append, although this isn't so clean either. 您也可以在附加之后立即从第二个视图控制器调用它,尽管这也不是很干净。 You could also implement a method inside your collection view controller that is called by the next view controller whenever it wants to append something that includes the item you want to add as a parameter instead of directly adding it to the other controller's array. 您还可以在集合视图控制器中实现一个方法,该方法将在下一个视图控制器想要添加包含要添加为参数的项目的东西时添加,而不是直接将其添加到另一个控制器的数组中。

Retaining a reference to the presenting view controller and accessing its data directly is also not good practice. 保留对呈现视图控制器的引用并直接访问其数据也不是一个好习惯。 What you really should be using is a delegate method (which is similar to the suggestion above, except more flexible since the other view controller doesn't need to know anything about its delegate other than that it implements the delegate protocol). 您真正应该使用的是一个委托方法(与上面的建议类似,但是更加灵活,因为另一个视图控制器除了实现委托协议外,不需要了解其他有关其委托的信息)。 More info here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM