UITableview 中的 UICollectionView - 获取被点击的 UICollectionView 的标签 [英] UICollectionView in UITableview - Get tag of tapped UICollectionView
问题描述
我在 UITableView
中添加了 UICollectionView
.现在,我想确定用户点击哪个单元格以及该 UICollectionView
的 indexPath.
I have added UICollectionView
in UITableView
. Now, I want to identify on which cell the user taps and the indexPath of that UICollectionView
.
我得到了正确的 indexPath
但无法获得他点击的 UICollectionView 的标签.我正在做这样的事情来获得点击
I'm getting correct indexPath
but could not get the tag of on which the UICollectionView he tapped. I'm doing something like this to get tap
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)
}
}
推荐答案
您的 tableView
中有多个部分,因此您可以在 NSIndexPath
中创建一个 NSIndexPath
实例code>UserLibraryTableViewCell 并像这样在 cellForRowAtIndexPath
中初始化它.
You are having multiple section in your tableView
, so you can create one NSIndexPath
instance in your UserLibraryTableViewCell
and initialize it in the cellForRowAtIndexPath
like this.
class UserLibraryTableViewCell: UITableViewCell {
var tableIndexPath: NSIndexPath?
}
现在在 cellForRowAtIndexPath
中设置这个 tableIndexPath
.
Now set this tableIndexPath
in the cellForRowAtIndexPath
.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell
cell.tableIndexPath = indexPath
return cell
}
现在在collectionView
的didSelectItemAtIndexPath
中像这样访问tableIndexPath
.
Now in didSelectItemAtIndexPath
of collectionView
access tableIndexPath
like this.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("\(self.tableIndexPath) ---- \(indexPath.item) ")
}
这篇关于UITableview 中的 UICollectionView - 获取被点击的 UICollectionView 的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!