콜렉션 뷰 셀을 사용자가 선택할 때 셀을 추가하려고합니다. 그는 이름을주고 저장하는 별도의보기 컨트롤러에서이 작업을 수행합니다. 초기 뷰 컨트롤러로 돌아 오면 새 셀이 콜렉션 뷰에 추가되지 않습니다. 여기 콜렉션 뷰에 셀을 추가하지 않았습니다.
는 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
}
}
다음 뷰 컨트롤러 : 당신은 컬렉션 뷰를
self.collectionView.reloadData
를 다시로드해야 또는 대안 당신이 할 수있는
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)
}
}
코드의 대부분이 좋아 보인다. 콜렉션 뷰를'collectionView'의'viewWillAppear'에서 다시로드 해보십시오. –