![](/img/trans.png)
[英]Swift - Passing data from a CollectionView to a TableViewController
[英]passing data from Tableview to CollectionView SWIFT
我试图传递一个数据,每当我点击用户时,它都会显示在 collectionView 上。 但是,我不知道为什么无论我单击哪个 tableview 行,它都会添加我的顶级用户,然后是第二个等等。我应该怎么做? (我的 collectionView 和 tableView 都在同一个视图上)
我使用协议将数据从 tableview 发送到 collectionView
protocol toRefresh {
func updateCollectionView() }
我声明了一个变量来存储选定的用户:
var chosenUser: [User] = []
至于我的 tableView
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let user = users[indexPath.row]
chosenUser.append(user)
updateCollectionView()
}
}
在我的收藏视图中
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return chosenUser.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.collectionView.deleteItems(at: [indexPath])
self.chosenUser.remove(at: indexPath.item) // here crashes as well
}
func updateCollectionView() {
collectionView.reloadData()
}
要从集合中删除一个项目,它必须首先从数据源中删除
self.chosenUser.remove(at: indexPath.row)
self.collectionView.deleteItems(at: [indexPath])
检索
let selected = users.index(of:chosenUser[indexPath.row])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.