我有一个集合视图单元,可以将数据传递给详细的视图控制器。单击单元格后,它会进入具有更多详细信息的视图控制器。在单元格中,我有一个按钮,当单击按钮时,它还会分割成详细的视图控制器,但是单击单元格时会有一个不同的视图控制器。
这就是我的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 ==用户的个人资料我在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)
}
答案 0 :(得分:1)
很明显,你正在遭遇崩溃,因为你的按钮操作正在调用xmlns:local="clr-namespace:WpfTests_2"
现在正在传递的传感器performSegue(withIdentifier: "UsersProfile", sender: self)
表示当前控制器的引用而不是self
你的需要获取该单元格的indexPath并将其传递,现在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
,将UsersProfile
强制转换为sender
,或者只是将我的条件替换为我的条件。
IndexPath