swift - 使用单元格中的按钮转到详细 View Controller

标签 swift segue uicollectionviewcell

我有一个 Collection View 单元格,它将数据传递给详细 View Controller 。单击单元格时,它会转入包含更多详细信息的 View Controller 。在单元格中,我有一个按钮,当单击该按钮时,它也会进入一个详细的 View Controller ,但与单击单元格时不同的 View Controller 。

这就是我的 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 Collection View 函数中创建的

    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)

当我单击该单元格时,它运行良好并将我引导至详细 View Controller ,但是当我单击该单元格内的按钮时,出现错误。 enter image description here

这是我的 editTappedButton 函数

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

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

        }

最佳答案

很明显,您遇到了崩溃,因为通过您的按钮操作,您正在调用 performSegue(withIdentifier: "UsersProfile", sender: self) 现在您正在通过 sender 传递 self 表示当前 Controller 的引用而不是 UICollectionViewCell 您需要的是获取该单元格的 indexPath 并传递它,现在在 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)
    }
}

关于swift - 使用单元格中的按钮转到详细 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43911805/

相关文章:

arrays - Swift 数组 var 问题

ios - 带有旋转图像的动画

ios - 之间的技术区别是什么? &!用快速的语言?

ios - 确定哪个 segue 触发了 UIViewController

swift - 尝试执行 segue 导致 "terminating with uncaught exception of type NSException"

ios - 单元格中的图像不出现

ios - UICollectionView 与 UIWebView 尺寸问题

swift - 如何将位图图像保存为 png 和/或 pdf 文件

ios - 使用 pushViewController 传递数据?

objective-c - 返回整数作为节中项目数时出错