2016-09-07 3 views
2

Я добавил UICollectionView в UITableView. Теперь, я хочу определить, на чью ячейку пользователь набирает и indexPath этого UICollectionView.UICollectionView в UITableview - Получить тег tapped UICollectionView

Я получаю правильный indexPath, но не смог получить тег, на который он прослушивал UICollectionView. Я делаю что-то вроде этого, чтобы получить Коснитесь

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    print("\(collectionView.tag) ---- \(indexPath.item) ") 
} 

весь код

class UserLibraryViewController: UIViewController { 

extension UserLibraryViewController : UITableViewDelegate { } 

extension UserLibraryViewController : UITableViewDataSource { 

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

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

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

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

TableViewCell

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

extension UserLibraryTableViewCell : UICollectionViewDataSource { 

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

    return 12 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("userBooksCollectionViewCell", forIndexPath: indexPath) as! UserLibraryCollectionViewCell 



    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    print("\(collectionView.tag) ---- \(indexPath.item) ") 


} 
} 

extension UserLibraryTableViewCell : 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) 
} 

} 

ответ

2

Вы испытываете несколько раздел в вашем tableView, так что вы можете создать один NSIndexPath экземпляр в вашем UserLibraryTableViewCell и инициализировать его в cellForRowAtIndexPath как это.

class UserLibraryTableViewCell: UITableViewCell { 

    var tableIndexPath: NSIndexPath? 

} 

Теперь установите этот tableIndexPath в cellForRowAtIndexPath.

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

Сейчас в didSelectItemAtIndexPath из collectionView доступа tableIndexPath как это.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    print("\(self.tableIndexPath) ---- \(indexPath.item) ") 
} 
+0

Спасибо. Это решило мою проблему. – Nitesh

+0

Хорошее решение;), рекомендуется. –

+1

Спасибо mate :) У меня отличный день :) –

0

Попробуйте это:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell 
    cell.collectionView.tag = indexpath.row 
    return cell 
} 


func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return myBooksGenre.count 
} 

Вы не устанавливаете Tag для вида коллекции для конкретной ячейки в cellForRowAtIndexPath вида таблицы.

Обновить код.

UICollectionViewDelegate соответствуют Также протокол:

extension UserLibraryTableViewCell : UICollectionViewDataSource, UICollectionViewDelegate 
+0

Да. Пробовал это, но все же его показ «0» всем. – Nitesh

+0

'print (" \ (collectionView.tag) ---- \ (indexPath.item) ") Выполняется строка'? –

+0

Да, потому что 0 означает, что вы выбираете 0-я ячейка таблицыView. Я думаю, что также 'numberOfRowsInSection' и' numberOfRowsInSection' являются неуместными. проверьте код обновления. –

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