2014-12-09 3 views
0

콜렉션 뷰 셀을 사용자가 선택할 때 셀을 추가하려고합니다. 그는 이름을주고 저장하는 별도의보기 컨트롤러에서이 작업을 수행합니다. 초기 뷰 컨트롤러로 돌아 오면 새 셀이 콜렉션 뷰에 추가되지 않습니다. 여기 콜렉션 뷰에 셀을 추가하지 않았습니다.

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


} 
+0

코드의 대부분이 좋아 보인다. 콜렉션 뷰를'collectionView'의'viewWillAppear'에서 다시로드 해보십시오. –

답변

0

reloadItemsAtIndexPath을 사용하면 전체 컬렉션보기를 다시로드하지 않으려는 경우/새 항목의 indexPath를 알고 있다고 가정합니다.

이제 질문은 언제 전화할까요?

현재 구현하기에 가장 쉬운 것은 viewWillAppear이지만, 다시로드 할 필요가 없을 때 (예 : 처음보기가 표시되거나 다음보기 컨트롤러가 아무 것도 추가하지 않고 반환하는 경우) 컬렉션보기를 다시로드합니다. 등등 - 컬렉션 뷰가 작 으면 큰 효과가 없지만 쉽게 수행 할 수 있습니다.) 추가 한 직후에 두 번째보기 컨트롤러에서 호출 할 수도 있지만 너무 깨끗하지는 않습니다. 다른 컨트롤러의 배열에 직접 추가하는 대신 매개 변수로 추가하려는 항목이 포함 된 것을 추가 할 때마다 다음보기 컨트롤러에서 호출하는 컬렉션보기 컨트롤러 내부에 메서드를 구현할 수도 있습니다.

프리젠 테이션 컨트롤러에 대한 참조를 유지하고 해당 데이터에 직접 액세스하는 것도 좋지 않습니다. 당신이 정말로 사용해야하는 것은 위의 제안과 비슷하지만 다른 뷰 컨트롤러는 델리게이트 프로토콜을 구현하는 것 외의 다른 델리게이트에 대해 알 필요가 없으므로 더 유연합니다. 더 많은 정보 here

+1

정말 고마워요! :) – Seynin97

관련 문제