【发布时间】: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