【问题标题】:Segue from collection view cell to table view cell从集合视图单元到表格视图单元
【发布时间】:2017-05-14 04:42:29
【问题描述】:

这让我很难过。我正在尝试从集合视图单元到表格视图控制器。当我单击一个单元格(特别是单元格内的一个按钮)时,我应该能够转到表格视图控制器并在表格视图控制器的单元格中单击该单元格的详细信息。

在带有集合视图单元格的视图控制器中,我实现了一个 segue。

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {       
        }
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "details" {
    if segue.identifier == "UsersProfile" {

                if let indexPath = sender as? IndexPath{
                    let vc = segue.destination as! UsersProfileViewController
                    let post = self.posts[indexPath.row] as! [String: AnyObject]
                    let posted = self.posts[indexPath.row] as! [NSMutableArray: AnyObject]
                    let username = post["username"] as? String
                    let userpicuid = post["uid"] as? String
                    let userpostuid = posted["uid"] as? NSMutableArray
                    vc.username = username
                    vc.userpicuid = userpicuid
                    vc.posts = userpostuid
                     print(indexPath.row)
                }
            }}

当我继续时,用户图片和用户名会很好地出现在详细的表格视图中,但它们不会在表格视图单元格中查看。这是我的表格视图控制器代码:

     var posts = NSMutableArray()
 @IBOutlet var TableView: UITableView!

     func numberOfSections(in tableView: UITableView) -> Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return self.posts.count

              }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "UsersCell", for: indexPath) as! UserProfileTableViewCell

            //Configure the cell


            print(posts[indexPath.row])
            let post = self.posts[indexPath.row] as! [String: AnyObject]
            cell.Title.text = post["title"] as? String
            cell.Author.text = post["Author"] as? String
            cell.Price.text = post["Price"] as? String

            if let imageName = post["image"] as? String {

                let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")


                imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                    if error == nil {


                    }else {

                        print("Error downloading image:" )
                    }})}

            return cell
            }

表格视图单元格中没有显示任何内容。我是 swift 新手,我一直在努力寻找解决方案,但我做不到。

这是我的数据库的图片:

【问题讨论】:

  • 无需在didselectitem内部实现prepareforsegue

标签: swift uitableview firebase-realtime-database uicollectionview


【解决方案1】:

你有没有把你的tableview的Delegate和Datasource添加到类中?

【讨论】:

  • 我确实设置了委托和数据源
【解决方案2】:

我创建了一个示例,当用户点击UICollectionViewCell 时,它会将您导航到tableViewController

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

// Change it with your required items.
return 10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath as IndexPath)

// Do not forgot to set cell tag.
cell.tag = indexPath.row//cell tag has been set to indexpath.row

return cell
}

为了导航到您的tableViewController,请执行PrepareForSegue,如下所述。

override func prepare(for segue: UIStoryboardSegue, sender: Any!) {

if segue.identifier == "UsersProfile" {

// Use this tag to pass data for the selected cell
//this tag will act as indexpath.row

guard let tag = (sender as? UIView)?.tag
else {
 return
}

// add the data you want to parse here.
print(tag)
}
}

注意:确保您已将故事板文件中的 segue 从 UICollectionViewCell 设置为 UITableViewController

如果您需要更多详细信息,请告诉我,我会将其添加到答案中。

【讨论】:

  • 抱歉,我认为我的问题不够清楚。我已经工作正常。我能够成功地从我的 collectionview 转移到 tableviewcontroller。我知道我成功地传递了数据,因为我能够解析两个数据。问题是我希望在 tableviewcontroller 的 tableviewcell 中显示一些数据,但它不起作用。例如,我可以将 tableviewcell 之外的数据解析为标签,但我很难将其解析为实际的 tableviewcell @Maddy
  • 您的self.posts 数组中的数据是什么,因为您没有将任何数据传递给 post 数组。
  • 我正在单击单元格,并且单元格是我数据库中的特定用户,通过单击单元格,我想进入一个 tableviewcontroller,其中包含由该特定用户发布的所有帖子的列表用户显示在 tableviewcell 中。 self.posts 丢失了特定用户发表的所有帖子
  • 你得到空记录。与帖子数组中的虚拟数据共享单个项目
  • 当我想将数据从 collectionviewcells 传递到我的 tableviewcell 并将其显示在标签中时,我可以很容易地在单击单元格时使用这个简单的代码将名称传递给标签我的 tableviewcontroller self.Usersname.text = self.username 中的 viewDidLoad。我只是很难将数据传递给 tableviewcell