[英]pass data from collection view to table view in the same view controller:
If i click on a particular cell of collection view then the data should be shown related to that cell of collection view in the table view in the same view controller (not in the other view controller)如果我单击集合视图的特定单元格,则应在同一视图 controller 的表视图中显示与该集合视图单元格相关的数据(不在其他视图控制器中)
On didSelectItemAt
method of collectionView you reset the data source of tableView and reload tableView that should do the job.在 collectionView 的
didSelectItemAt
方法上,您重置 tableView 的数据源并重新加载应该完成工作的 tableView。
Regardless of having one or multiple view controllers.不管有一个或多个视图控制器。 A good practice is to have a data structure that fits your visual state.
一个好的做法是拥有一个适合您的可视化 state 的数据结构。 For your case I would expect to have something like
对于你的情况,我希望有类似的东西
var collectionViewItems: [Any]
var tableViewItems: [Any]
But to be more concrete let's assume that we have a collection view of users where after pressing a certain user a table view should update a list of friends for that user.但更具体地说,让我们假设我们有一个用户集合视图,在按下某个用户后,表格视图应该更新该用户的朋友列表。
A data source like the following could show that:像下面这样的数据源可以表明:
struct User {
let name: String
let friends: [User]
}
Now to create a structure that is more fit for your display you could have something like this:现在要创建一个更适合您的显示的结构,您可以使用以下内容:
class UserFriendsDataModel {
let allUsers: [User]
var selectedUser: User?
init(allUsers: [User]) { self.allUsers = allUsers }
}
In your view controller you would create a new instance of your data model.在您的视图 controller 中,您将创建数据 model 的新实例。 Probably in
viewDidLoad
but this all depends on how you collect your data.可能在
viewDidLoad
中,但这一切都取决于您如何收集数据。
For instance例如
class ViewController: UIViewController {
private var dataModel: UserFriendsDataModel?
override func viewDidLoad() {
super.viewDidLoad()
dataModel = .init(allUsers: [
.init(name: "Me", friends: [.init(name: "You", friends: [])])
])
}
}
Now your data source implementations can use dataModel?.allUsers
for collection view and dataModel?.selectedUser?.friends
for your table view.现在您的数据源实现可以将
dataModel?.allUsers
用于集合视图,将dataModel?.selectedUser?.friends
用于您的表视图。
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dataModel?.selectedUser?.friends.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = dataModel?.selectedUser?.friends[indexPath.row].name
return cell
}
}
Now all that is left is interaction.现在剩下的就是交互。 When a collection view cell is pressed you would do
当按下集合视图单元格时,您会这样做
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let user = dataModel?.allUsers[indexPath.row]
dataModel?.selectedUser = user
tableView?.reloadData()
}
}
note here that a new user is being selected using selectedUser = user
and then a reload is triggered for table view calling tableView?.reloadData()
which will force the table view to call data source methods and get the new information.请注意,正在使用
selectedUser = user
选择新用户,然后为调用tableView?.reloadData()
的表视图触发重新加载,这将强制表视图调用数据源方法并获取新信息。
An even better approach may be to listen for changes in selected user by creating your own delegates on your data model and respond to that.更好的方法可能是通过在数据 model 上创建自己的委托来监听所选用户的变化并做出响应。 But that is already out of scope for this question.
但是对于这个问题,这已经超出了 scope 的范围。
I hope this puts you on the right path.我希望这能让你走上正确的道路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.