首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionView in UITableview -点击UICollectionView的Get标记

UICollectionView in UITableview -点击UICollectionView的Get标记
EN

Stack Overflow用户
提问于 2016-09-07 06:03:23
回答 2查看 1.3K关注 0票数 2

我在UICollectionView中添加了UITableView。现在,我想识别用户点击的单元格和UICollectionViewUICollectionView

我得到了正确的indexPath,但无法得到标记的UICollectionView,他点击。我做这样的事是为了得到水龙头

代码语言:javascript
复制
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("\(collectionView.tag) ---- \(indexPath.item) ")
}

全码

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
}

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-07 06:13:25

您的tableView中有多个部分,因此您可以在UserLibraryTableViewCell中创建一个NSIndexPath实例,并像这样在cellForRowAtIndexPath中初始化它。

代码语言:javascript
复制
class UserLibraryTableViewCell: UITableViewCell {

    var tableIndexPath: NSIndexPath?

}

现在,在tableIndexPath中设置这个cellForRowAtIndexPath

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell
    cell.tableIndexPath = indexPath
    return cell
}

现在在didSelectItemAtIndexPath of collectionView access tableIndexPath中是这样的。

代码语言:javascript
复制
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

     print("\(self.tableIndexPath) ---- \(indexPath.item) ")
}
票数 2
EN

Stack Overflow用户

发布于 2016-09-07 06:06:29

试试这个:

代码语言:javascript
复制
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

更新代码。

还符合UICollectionViewDelegate协议:

代码语言:javascript
复制
extension UserLibraryTableViewCell : UICollectionViewDataSource, UICollectionViewDelegate
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39362240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档