2016-08-04 17 views
1

こんにちは私はUICollectionViewの中にセルタイトルを入れたいと思います。UICollectionViewセルには1つのセルがあります。UICollectionViewアイテムを取得したいセルのタイトルを取得します。ここに私のコード。コレクションビュー内の2方向セル、セルタイトルを取得する方法?

ビューコントローラセル

import UIKit 

class ViewController: UIViewController { 
    var categories = ["A", "B", "C", "D", "E"] 
} 

extension ViewController : UITableViewDelegate { } 

extension ViewController : UITableViewDataSource { 

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return categories[section] 

    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return categories.count 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow 
     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     print(indexPath.row) 

    } 

} 

UICollectionView 


import UIKit 

class CategoryRow : UITableViewCell { 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

extension CategoryRow : UICollectionViewDataSource { 


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 12 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as! VideoCell 
     return cell 
    } 

} 

extension CategoryRow : UICollectionViewDelegateFlowLayout { 


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     let itemsPerRow:CGFloat = 4 
     let hardCodedPadding:CGFloat = 5 
     let itemWidth = (collectionView.bounds.width/itemsPerRow) - hardCodedPadding 
     let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) 
     return CGSize(width: itemWidth, height: itemHeight) 

    } 


    //MARK :- UICollectionViewDelegate 
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 


    print(indexPath.row) // HERE gives collection view number when clicked BUT I WANT TO Get cell title which inside categories ARRAY example: A/3, B/5 



    } 




} 

ボトム側は、私が

プリント(indexPath.row)が付い//ここクリックコレクションビュー数を与えますが、私は、カテゴリ内のセルのタイトルを取得したいですARRAY例:A/3、B/5

私はそこに行動を見たい、助けてくれてありがとう!

答えて

1

私はあなたのクラスに

CategoryRow : UITableViewCell { 
    var title: String? 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

をタイトルprotperyを追加必要があり、ViewControllerセットにCategoryRow

func prepareForReuse() { 
    super.prepareForReuse() 
    title = nil 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow 
     let titleFromCategories = categories[indexPath.row] as? String 
     cell.title = titleFromCategories 
     return cell 
    } 

を細胞には、このタイトルをメソッドを追加必要があると思うし、 CategoryRowでは以下のように実装します

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     let yourString = title + String(format: "/%d", indexPath.row) 

     print(yourString) // HERE gives collection view number when clicked BUT I WANT TO Get cell title which inside categories ARRAY example: A/3, B/5 

} 
+0

こんにちは私でしたが、私がクリックしたとき/ 7、すべてのセルが与える、/ 3/5のような出力が得られますが、I Cをクリックしましたが、すべてを返しますA – SwiftDeveloper

+0

セルに 'prepareforReuse'メソッドを追加する必要があります。更新された回答 – iSashok

+0

私は同じものを追加しました。出力はすべてA/2のようになります – SwiftDeveloper

1

次のようにあなたの配列からタイトルを取得します。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    let title = String(format: "%@/%d", categories[indexPath.row], indexPath.row) 
    print(title) 
} 
+0

}エラーが言うFUNC オーバーライド – SwiftDeveloper

関連する問題