【发布时间】:2020-01-08 10:17:00
【问题描述】:
我是 iOS 新手,我想在 UITableView 中实现一个 UICollectionView,它可以在 UITableview 的第 1 部分中进行多个选择/取消选择。在第 2 节中,只允许进行一次选择。如果我关闭 Viewcontroller 并且当我再次打开它时,它必须将最后选择的单元格显示为突出显示,并保存选择状态事件。
我搜索了教程,但所有教程都没有提到选择/取消选择集合单元格的状态或在关闭视图控制器后保存状态。
有人可以帮忙实现吗?
提前致谢!
这是我到现在为止的代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return clvData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "clvCell", for: indexPath) as! demoCollectionViewCell
cell.title.text = clvData[indexPath.item] as? String
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = .red
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = .white
}
你们也可以在这里查看我的项目: https://mega.nz/#!xRs0EQyQ
【问题讨论】:
-
能否请您在此处发布一些您到目前为止所做的代码。
-
我添加了我选择的代码并上传了我当前的演示项目。你可以检查一下。
-
要进行多项选择,请查看here。并且对于单一选择,将数组替换为单个对象/数据而不是数组。
-
那么,关闭视图控制器然后再次打开时保存选择状态如何?
标签: ios swift uitableview uicollectionview