這是我的 CollectionViews 代碼。到目前為止,我有兩個 CollectionView,正如您在代碼中的字串中看到的那樣,它們每個都有 6 個按鈕。我需要從字串中呼叫按鈕,以便在按下它們時會打開一個新視窗。新視窗將包含諸如抽認卡之類的內容,這些內容與所按下按鈕的主題有關。
供參考,
是模擬器的樣子。
代碼:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class FirstCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var buttonOne: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
func commonInit() {
guard buttonOne != nil else { return }
buttonOne.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
buttonOne.layer.cornerRadius = 10
buttonOne.clipsToBounds = true
buttonOne.layer.borderWidth = 1.0
buttonOne.layer.borderColor = UIColor.white.cgColor
}
}
class SecondCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var buttonTwo: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
commonInit()
}
func commonInit() {
guard buttonTwo != nil else { return }
buttonTwo.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
buttonTwo.layer.cornerRadius = 10
buttonTwo.clipsToBounds = true
buttonTwo.layer.borderWidth = 1.0
buttonTwo.layer.borderColor = UIColor.white.cgColor
}
}
class TwoCollectionsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var firstCV: UICollectionView!
@IBOutlet weak var secondCV: UICollectionView!
let firstData: [String] = [
"Good Work", "Nice Try", "Btn 3", "Btn 4", "Btn 5", "Btn 6"]
let secondData: [String] = [
"Second 1", "Second 2", "Second 3", "Second 4", "Second 5", "Second 6"]
override func viewDidLoad() {
super.viewDidLoad()
firstCV.dataSource = self
firstCV.delegate = self
secondCV.dataSource = self
secondCV.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == firstCV {
return firstData.count
}
return secondData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == firstCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath) as! FirstCollectionViewCell
cell.buttonOne.setTitle(firstData[indexPath.item], for: [])
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
cell.buttonTwo.setTitle(secondData[indexPath.item], for: [])
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == firstCV {
print("Selected Item: \(indexPath.item) in First CV")
return
}
print("Selected Item: \(indexPath.item) in Second CV")
}
}
uj5u.com熱心網友回復:
只是在didSelectItemAt寫一個開關盒。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == firstCV {
handleCases(text : firstData[indexPath.item])
} else {
handleCases(text : secondData[indexPath.item])
}
}
func handleCases(text : String) {
switch text {
case firstData[0]:
print("clicked \(firstData[0])
// write here code to go to details
break
case firstData[1]:
print("clicked \(firstData[0])
break
// put all 6 cases of firstData and secondData and then default
}
}
另一種解決方案是在單元格內添加 IBAction 并handleCases(text : String)在此 IBAction 中的單元格內呼叫。
uj5u.com熱心網友回復:
看起來您只@IBOutlet為按鈕定義了變數。創建@IBAction函式并將“打開新視窗”(即執行segues)的代碼放入該函式中。
網上有很多資源解釋了如何做到這一點。其中之一就是這個。
uj5u.com熱心網友回復:
我發現最好對兩個按鈕使用@IBAction 回呼函式,而不是委托回呼函式aka(didSelect)
首先,您可以在 (TwoCollectionsViewController) 中創建 @IBAction,然后將兩者 (buttonOne, buttonTwo) 鏈接到此函式,同時當用戶按下按鈕時,您可以知道按下了哪個按鈕(取決于您的用例),為此設定按鈕的 id 是最好的選擇,您可以在回傳單元格 in(cellForRowAt) 之前將此行添加到 Collection view button(buttonOne, buttonTwo): cell.buttonOne.restorationIdentifier = "(indexPath.row)"
然后加:
if let btn = sender as? UIButton {
print(btn.restorationIdentifier)
}
到@IBAction func,之后你可以使用:
performSegue(withIdentifier: "Your Identifier", sender: btn)
override: prepare(for segue: UIStoryboardSegue, sender: Any?)function 然后您可以在(準備功能)中配置目標視圖控制器
希望這可以解決您的問題:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492864.html
標籤:IOS 迅速 代码 uicollectionview 按钮
