2016-08-04 28 views
1

你好我想在UICollectionView裏面得到單元格的標題,我內部有1個單元格UICollectionView單元格有類別數組當我點擊UICollectionView項目我想要單元格的標題。我的代碼在這裏。集合視圖中的兩個定向單元格,如何獲取單元格標題?

視圖控制器細胞

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 



    } 




} 

底側I標記

打印(indexPath.row)//這裏給出點擊時集合視圖數,但我想獲取其內部類別細胞標題ARRAY示例:A/3,B/5

我想在那裏看到行動,感謝您的幫助!

回答

1

我想你需要標題protpery添加到您的類

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

,你需要在CategoryRow

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

ViewController集中添加方法這個標題細胞

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

你好,我做了,但是當我點擊給出像A/5的輸出,A/3,A/7,每一個細胞都給出了,但我點擊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

說誤差:使用未解決的識別符 '類別' – SwiftDeveloper

相關問題