【问题标题】:Cell not being added to collection view单元格未添加到集合视图
【发布时间】:2014-12-09 15:46:06
【问题描述】:

我正在尝试在用户选择时添加一个集合视图单元格。他在一个单独的视图控制器中执行此操作,并在其中给出名称并保存它。但是当它返回到初始视图控制器时,新单元格不会被添加到集合视图中。

这是我的 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
}
}

下一个视图控制器:

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)
}


}

【问题讨论】:

  • 大部分代码看起来不错...尝试在viewWillAppear of collectionView 中重新加载集合视图。

标签: ios swift uicollectionview


【解决方案1】:

您需要重新加载集合视图

self.collectionView.reloadData

或者,如果您不想重新加载整个集合视图/想要显示动画,则可以使用 reloadItemsAtIndexPath,假设您知道新项目的 indexPath。

现在的问题是,你什么时候调用这个?

对于您当前的实现来说,最简单的事情是在viewWillAppear 中,但是当不需要重新加载集合视图时(例如,第一次出现视图时,或者如果下一个视图控制器返回而不附加任何东西等等——如果你的收藏视图很小,这没什么大不了的,但它可以很容易地做得更好)。您也可以在追加后立即从第二个视图控制器调用它,尽管这也不是那么干净。您还可以在集合视图控制器中实现一个方法,只要下一个视图控制器想要附加包含您要作为参数添加的项目的内容,而不是直接将其添加到另一个控制器的数组中,该方法就会被下一个视图控制器调用。

保留对呈现视图控制器的引用并直接访问其数据也不是好的做法。你真正应该使用的是委托方法(这类似于上面的建议,除了更灵活,因为另一个视图控制器不需要知道任何关于它的委托,除了它实现委托协议)。更多信息here

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    相关资源
    最近更新 更多