【问题标题】:Two directional cell inside collection view , how to get cell title?集合视图内的两个定向单元格,如何获取单元格标题?
【发布时间】:2016-08-04 07:42:55
【问题描述】:

您好,我想在 UICollectionView 中获取单元格标题,当我单击 UICollectionView 项目时,我在 UICollectionView 单元格中有 1 个单元格有类别数组我想获取单元格标题。我的代码在这里。

视图控制器单元格

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



    }




}

我标记的底部

print(indexPath.row) // 点击此处给出集合视图编号,但我想获取类别数组中的单元格标题,例如:A/3、B/5

我想在那里看到行动,谢谢你的帮助!

【问题讨论】:

    标签: ios swift2 uicollectionview uicollectionviewcell


    【解决方案1】:

    我认为你需要为你的班级添加标题属性

    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
    
     }
    

    【讨论】:

    • 你好,我做到了,但是当我单击时会给出 A/5、A/3、A/7 之类的输出,每个单元格都给出 A,但我点击了 C,但所有单元格都给出了 A
    • 我忘了,你需要在单元格中添加prepareforReuse 方法,更新答案
    • 我添加了,但同样给出了所有输出,A/2 之类的
    • 我的类别行文件顶部编辑那是:class CategoryRow : UITableViewCell { var title: String? @IBOutlet 弱 var collectionView:UICollectionView!覆盖 func prepareForReuse() { super.prepareForReuse() title = nil } }
    【解决方案2】:

    从您的数组中获取标题,如下所示:

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
         let title = String(format: "%@/%d", categories[indexPath.row], indexPath.row) 
         print(title)
    }
    

    【讨论】:

    • 说错误:使用未解析的标识符“类别”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多