Swift 单击uicollection单元格中的按钮继续

Swift 单击uicollection单元格中的按钮继续,swift,uibutton,uicollectionviewcell,uistoryboardsegue,Swift,Uibutton,Uicollectionviewcell,Uistoryboardsegue,在collectionviewcell中有一个按钮,当我单击该按钮时,目标是切换到更详细的视图控制器 class WelcomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,UIGestureRecognizerDelegate, UISearchBarDelegate { @IBOutlet weak var CollectionView: UIColl

在collectionviewcell中有一个按钮,当我单击该按钮时,目标是切换到更详细的视图控制器

 class WelcomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,UIGestureRecognizerDelegate, UISearchBarDelegate {


    @IBOutlet weak var CollectionView: UICollectionView!


    var databaseRef = FIRDatabase.database().reference()
    var loggedInUser = FIRAuth.auth()?.currentUser
    var dictDetails: [String:AnyObject]?
    var posts = NSMutableArray()
    let storage = FIRStorage.storage()


    override func viewDidLoad() {
        super.viewDidLoad()
        (CollectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize = CGSize(width: (self.view.frame.width - 10) / 2.4, height: (self.view.frame.width - 10) / 1.5 )
        self.navigationItem.title = "Lit Swap"
        CollectionView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 300)
        self.CollectionView.contentInset = UIEdgeInsetsMake(-65, 0, 0, 0)
        definesPresentationContext = true

        loadData()
    }


    @IBAction func editButtonTapped() -> Void {
        print("Hello Edit Button")
        performSegue(withIdentifier: "UsersProfile", sender: self)

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


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


               if let indexPaths = self.CollectionView!.indexPathsForSelectedItems{

                ///I DONT THINK YOU SHOULD REFERENCE POST, REFERENCE BOOKS INSTEAD///////////////

                  //  if let indexPaths = self.CollectionView.cellForItem(at: <#T##IndexPath#>){


                    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)

                         }} }


    }


    func loadData(){
        if (FIRAuth.auth()?.currentUser) != nil{
            FIRDatabase.database().reference().child("books").observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

                let loggedInUserData = snapshot
                if let postsDictionary = snapshot .value as? [String: AnyObject] {
                    for post in postsDictionary {
                        self.posts.add(post.value)
                    }
                    self.CollectionView.reloadData()


                }})}
    }

    // Properties of the UICollectionView
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return self.posts.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {



        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PinterestLikeCollectionViewCell


        //configure the cell...

        if (FIRAuth.auth()?.currentUser) != nil{


            print(posts[indexPath.row])
            let post = self.posts[indexPath.row] as! [String: AnyObject]
            cell.Title.text = post["title"] as? String
            cell.Author.text = post["Author"] as? String

            let editButton = UIButton(frame: CGRect(x: 8, y: 225, width: 154, height: 45))

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

            cell.addSubview(editButton)

            if let imageName = post["image"] as? String {

                let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")


                imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                    if error == nil {

                        let image = UIImage(data: data!)
                        cell.Books.image = image 

                        cell.Books.roundCornersForAspectFit(radius: 10)
                        cell.Books.clipsToBounds = true


                    }else {

                        print("Error downloading image:" )
                    }})}}
        return cell
    }

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: CGFloat((collectionView.frame.size.width / 5) - 20), height: CGFloat(500))
    }


    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)

            }  }}

}
类WelcomeViewController:UIViewController、UICollectionViewDataSource、UICollectionViewDelegate、UIGestureRecogenerDelegate、UISearchBarDelegate{
@ibvar CollectionView:UICollectionView!
var databaseRef=FIRDatabase.database().reference()
var loggedInUser=FIRAuth.auth()?.currentUser
var dictDetails:[字符串:AnyObject]?
var posts=NSMutableArray()
let storage=FIRStorage.storage()
重写func viewDidLoad(){
super.viewDidLoad()
(CollectionView.collectionViewLayout为!UICollectionViewFlowLayout)。itemSize=CGSize(宽度:(self.view.frame.width-10)/2.4,高度:(self.view.frame.width-10)/1.5)
self.navigationItem.title=“轻交换”
CollectionView.frame=CGRect(x:0,y:0,宽度:self.view.frame.size.width,高度:300)
self.CollectionView.contentInset=UIEdgeInsetsMake(-65,0,0,0)
DefinePresentationContext=true
loadData()
}
@iAction func editButtonTapped()->Void{
打印(“Hello编辑按钮”)
performsgue(标识符为:“UsersProfile”,发送者:self)
func prepare(用于segue:UIStoryboardSegue,发件人:有吗?){
如果segue.identifier==“UsersProfile”{
self.navigationController?.navigationBar.titleTextAttributes=[NSForegroundColorAttributeName:UIColor.white]
如果让indexPaths=self.CollectionView!.indexPathsForSelectedItems{
///我认为你不应该参考文章,而应该参考书///////////////
//如果让indexPaths=self.CollectionView.cellForItem(位于:){
设vc=segue.destination为!UsersProfileViewController
let cell=发送方为!UICollectionViewCell
让indexPath=self.CollectionView!.indexPath(for:cell)
让post=self.posts[(indexPath?.row)!]as![String:AnyObject]
//让username=post[“username”]作为字符串
让userpicuid=post[“uid”]作为字符串
//vc.username=用户名
vc.userpicuid=userpicuid
打印(indexPath?.row)
}} }
}
func loadData(){
如果(FIRAuth.auth()?.currentUser)!=nil{
FIRDatabase.database().reference().child(“books”).observeSingleEvent(of:.值,其中:{(快照:FIRDataSnapshot))位于
让loggedInUserData=snapshot
如果让postsDictionary=snapshot.value为?[字符串:AnyObject]{
在postsDictionary中发布{
self.posts.add(post.value)
}
self.CollectionView.reloadData()
}})}
}
//UICollectionView的属性
func numberOfSections(在collectionView:UICollectionView中)->Int{
返回1
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回self.posts.count
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:“cell”,for:indexath)作为!PinterestLikeCollectionViewCell
//配置单元格。。。
如果(FIRAuth.auth()?.currentUser)!=nil{
打印(发布[indexath.row])
让post=self.posts[indexPath.row]作为![String:AnyObject]
cell.Title.text=post[“Title”]作为字符串
cell.Author.text=post[“Author”]作为?字符串
让editButton=UIButton(帧:CGRect(x:8,y:225,宽度:154,高度:45))
editButton.addTarget(自身,操作:#选择器(editButtonTapped),用于:UIControlEvents.touchUpInside)
editButton.tag=indexPath.row
打印(indexPath.row)
editButton.isUserInteractionEnabled=true
cell.addSubview(编辑按钮)
如果让imageName=post[“image”]作为字符串{
让imageRef=FIRStorage.storage().reference().child(“images/\(imageName)”)
imageRef.data(使用MaxSize:25*1024*1024,完成:{(数据,错误)->Void in
如果错误==nil{
让image=UIImage(数据:data!)
cell.Books.image=图像
单元格。书本。圆角透视图(半径:10)
cell.Books.clipstobunds=true
}否则{
打印(“下载图像时出错:”)
}})}}
返回单元
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽度:CGFloat((collectionView.frame.size.width/5)-20),高度:CGFloat(500))
}
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
}
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
如果segue.identifier==“详细信息”{
self.navigationController?.navigationBar.titleTextAttributes=[NSForegroundColorAttributeName:UIColor.white]
如果让indexPaths=self.CollectionView!.indexPathsForSelectedItems{
让vc=segue.destination为!BookDetailsViewController
let cell=发送方为!UICollectionViewCell
让indexPath=self.CollectionView!.indexPath(for:cell)
让post=self.posts[(indexPath?.row)!]as![String:AnyObject]
let Booked=以字符串形式发布[“标题”]
让Authors=post[“Author”]作为字符串
让ISBNS=post[“ISBN”]作为?字符串
让价格=