2016-08-04 2 views
1

Привет, я хочу получить название ячейки внутри UICollectionView, у меня есть 1 ячейка внутри UICollectionView ячейка имеет массив категорий, когда я нажимаю UICollectionView item Я хочу получить название ячейки. Мои коды здесь.Два направленных ячейки внутри коллекции зрения, как получить название ячейки?

Посмотреть контроллер Cell

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) // здесь дает коллекции номер вида при нажатии на, но я хочу получить название ячейки, которые внутри категории Пример массива: 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, А/3, А/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

Смежные вопросы