gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:39:29 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com