gpt4 book ai didi

swift - 单击 uicollection 单元格中的按钮进行继续

转载 作者:行者123 更新时间:2023-11-30 12:28:57 26 4
gpt4 key购买 nike

Collection View 单元中有一个按钮,当我单击该按钮时,目标是转到更详细的 View Controller 。

 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)

} }}

}

但是,它没有做任何事情。我目前的单元设置是,当您单击它时,它会转到详细的 View Controller 。但是单元格内的按钮在单击时应该转到不同的详细 View Controller 。对于单元格的 Segue,用于对 didSelectItemAt 函数中放入的内容进行 Segue 的信息。我不确定在单元格中包含按钮的转场的函数是什么。

最佳答案

您能否将您的类(class)作为一个整体包含进来,以便我可以更好地了解您如何配置 View ?我会用我认为有帮助的内容来更新我的答案,我认为可能会发生一些不同的事情导致这种情况。

更新

试试这个:

  1. 在 PinterestLikeCollectionViewCell 类中创建并添加按钮。
  2. 制定一个协议(protocol),告诉其委托(delegate)人在单击单元格按钮时执行您的操作。
  3. 将您的 WelcomeViewController 类指定为该委托(delegate)并遵守协议(protocol)。

关于swift - 单击 uicollection 单元格中的按钮进行继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43822031/

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