简体   繁体   English

在同一视图 controller 中将数据从集合视图传递到表视图:

[英]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.

相关问题 将数据从表视图传递到视图控制器 - Pass data from table view to view controller 使用委托将数据从Collection View Controller传递到View Controller - Pass data from Collection View Controller to View Controller using delegates 如何将数据数据从集合视图单元传递到视图控制器 - how to pass data data from collection view cell to view controller 如何将视图 controller 中的数据传递到表视图 controller? - How to pass the data from view controller to the table view controller? 如何将数据从集合视图传递到表视图类? - How to pass data from collection view to table view class? 在同一视图控制器中添加集合视图和表视图-Xamarin iOS - Adding a Collection View and a Table View in the same view controller - Xamarin iOS 如何将数据从表视图传递到另一个视图 controller - How to pass the data from table view to another view controller Swift-将数据从表视图Xib文件传递到视图控制器 - Swift - Pass data from table view xib file to view controller 如何将不同的数据从表视图传递到另一个视图 controller - How to pass the different data from table view to another view controller 将图像视图从集合视图传递到新的视图控制器 - Pass image view from collection view to a new view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM