【问题标题】:Segue to detailed view controller using a button in a cell使用单元格中的按钮转到详细视图控制器
【发布时间】:2017-05-11 09:41:16
【问题描述】:

我有一个将数据传递给详细视图控制器的集合视图单元。单击单元格时,它会进入具有更多详细信息的视图控制器。在单元格中,我有一个按钮,当单击该按钮时,它还会进入详细视图控制器,但与单击单元格时的视图控制器不同。

这就是我的 didselect 函数的样子。

  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "details" {


            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]




            if let indexPaths = self.CollectionView!.indexPathsForSelectedItems{

                let vc = segue.destination as! BookDetailsViewController
                let cell = sender as! UICollectionViewCell
                let indexPath = self.CollectionView!.indexPath(for: cell)
                let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
                let Booked = post["title"] as? String
                let Authors = post["Author"] as? String
                let ISBNS = post["ISBN"] as? String
                let Prices = post["Price"] as? String
                let imageNames = post["image"] as? String
                let imagesTwo = post["imageTwo"] as? String
                let imagesThree = post["imageThree"] as? String
                let imagesFour = post["imageFour"] as? String
                let imagesFive = post["imageFive"] as? String
                vc.Booked = Booked
                vc.Authors = Authors
                vc.ISBNS = ISBNS
                vc.Prices = Prices
                vc.imageNames = imageNames
                vc.imagesTwo = imagesTwo
                vc.imagesThree = imagesThree
                vc.imagesFour = imagesFour
                vc.imagesFive = imagesFive

                print(indexPath?.row)

            }  }

        if segue.identifier == "UsersProfile" {

            if let indexPaths = self.CollectionView!.indexPathsForSelectedItems{

                let vc = segue.destination as! UsersProfileViewController
                let cell = sender as! UICollectionViewCell
                let indexPath = self.CollectionView!.indexPath(for: cell)
                let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
                let username = post["username"] as? String
                let userpicuid = post["uid"] as? String
                vc.username = username
                vc.userpicuid = userpicuid


                print(indexPath?.row)

            }}}

如果 segue == User's Profile 我在 let cell = 行中出现错误。我在单元格中的按钮是在 cellForItemAt 集合视图函数中创建的

    let editButton = UIButton(frame: CGRect(x: 106, y: 171, width: 36, height: 36))

    editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside)
    editButton.tag = indexPath.row
    print(indexPath.row)
    editButton.isUserInteractionEnabled = true

    cell.addSubview(editButton)

当我单击单元格时,它可以完美运行并将我转到详细的视图控制器中,但是当我单击单元格中的按钮时,出现错误。

这是我的 editTappedButton 函数

    @IBAction func editButtonTapped() -> Void {
            print("Hello Edit Button")

performSegue(withIdentifier: "UsersProfile", sender: self)

        }

【问题讨论】:

  • 请发布错误
  • @publicstaticvoid 它已经发布了
  • 能否添加editButtonTapped功能。从错误来看,问题似乎出在您正在投射let cell = sender as! UICollectionViewCell 时。只打印 sender 的结果是什么?
  • 打印发件人是什么意思? @TristanBeaton
  • @juelizabeth 您是否在按钮操作中执行 segue?

标签: swift segue uicollectionviewcell


【解决方案1】:

很明显,您遇到了崩溃,因为您通过按钮操作调用performSegue(withIdentifier: "UsersProfile", sender: self),现在发送者通过self 表示当前控制器的引用而不是UICollectionViewCell,您需要的是获取索引路径该单元格并传递该单元格,现在在prepareForSegue 中将发件人转换为IndexPath 而不是UICollectionViewCell

首先将您的editButtonTapped 替换为以下一个

@IBAction func editButtonTapped(_ sender: UIButton) -> Void {
        print("Hello Edit Button")

    let point = sender.superview?.convert(sender.center, to: self.tableView)
    if let indexPath = self.tableView.indexPathForRow(at: point!) {
        performSegue(withIdentifier: "UsersProfile", sender: indexPath)
    }
}

现在在 prepareForSegue 中,对于标识符 UsersProfilesender 转换为 IndexPath 或简单地将您的条件替换为我的条件。

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 username = post["username"] as? String
        let userpicuid = post["uid"] as? String
        vc.username = username
        vc.userpicuid = userpicuid
        print(indexPath.row)
    }
}

【讨论】:

  • 完美运行!!非常感谢您的解决方案,尤其是解释!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 2013-08-16
  • 2017-12-20
  • 1970-01-01
相关资源
最近更新 更多