ios - 无法在 Collection View 中选择项目,Swift

标签 ios swift uicollectionview

我一直在使用 collectionViewsTableViews 来了解它们的工作原理,但这有点令人困惑。我有一个连接到 ViewController 的 Collection View 以及设置为自身的委托(delegate)和数据源。但是在尝试使用 didSelectItemAtIndexPath 时,没有任何反应。该集合也完美显示,因此数据源功能运行良好。

这是我的代码:

@IBOutlet weak var timeCollectionView: UICollectionView!

 override func viewDidLoad() {
        super.viewDidLoad()

        //Register the Cell
        let nib = UINib(nibName: "DurationSelectionCollectionViewCell", bundle:nil)
        self.timeCollectionView.registerNib(nib, forCellWithReuseIdentifier: "DurationSelectionCollectionViewCell")

        timeCollectionView.delegate = self
        timeCollectionView.dataSource = self

        ....

}

//Selecting an item and highlighting it. 
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        timeCollectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.grayColor()

}

最佳答案

如果 didSelectItemAtIndexPathItem: 没有被调用,请确保您已将 delegatedataSource 正确设置到您的 View Controller 。

关于ios - 无法在 Collection View 中选择项目,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37100905/

相关文章:

ios - Swift- 将索引路径数组转换为 Int 数组

ios - 录音获取文件大小

ios - Flutter + Firebase 抛出未处理的异常 : This widget has been unmounted Error Using Navigator to change view

swift - 从函数中检索结果(Swift/WatchOS 2)

ios - 在表格 View 和性能中滚动底部

ios - 如何改变UICollectionView的填充方向?

ios - 如何暂停我的应用程序足够长的时间以使我的屏幕显示为 "catch up"

iphone - 一个View中可以添加多少个UILabel

ios - 解析没有匹配项的 getObjectInBackgroundWithId 返回

ios - UICollectionViewCell 是方形的,但图像是矩形的