[英]update collectionview in swift, reload the data
我目前有兩個collectionview,每個都與一個字符串數組鏈接,並嵌入了按鈕。 我想這樣做,以便我可以 select 在底部的 collectionview 上的一個值,然后能夠將該值下降到頂部的 collectionview 上的 position,再次點擊它。 每次點擊按鈕時,我都不確定如何更新 collectionview。
import UIKit
class MyButtonCell: UICollectionViewCell{
@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var targetButton: UIButton!
var callback: (() -> ())?
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
contentView.layer.borderWidth = 1
contentView.layer.borderColor = UIColor.black.cgColor
}
@IBAction func buttonTapped(_ sender: UIButton) {
callback?()
}
}
class StevenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let buttonTitles: [String] = [
"4", "6", "7", "8"
]
var targetButtonTitles: [String] = [
"", "", "", ""
]
var current:String = ""
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var targetCollection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
targetCollection.delegate = self
targetCollection.dataSource = self
collectionView.delegate = self
collectionView.dataSource = self
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return buttonTitles.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
// set the button title (and any other properties)
if collectionView == self.collectionView {
// Setup here your cell
cell.callback = {
print("Button was tapped at \(indexPath)")
self.targetButtonTitles[indexPath.item] = self.buttonTitles[indexPath.item]
//print(self.targetButtonTitles)
self.current = self.buttonTitles[indexPath.item]
print(self.current)
// do what you want when the button is tapped
}
cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
return cell
} else {
// Setup here your targetCell
cell.callback = {
if self.current != ""{
self.targetButtonTitles[indexPath.item] = self.current
targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
}
}
targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
return targetCell
}
}
}
我不確定如何將“返回目標單元”放入 cell.callback 或 iBaction
我建議你通過集合視圖的標簽來做你的邏輯。
override func viewDidLoad() {
super.viewDidLoad()
collectionView.tag = 1
targetCollection.tag = 2
}
例如,在您的委托中,您可以通過檢查哪個集合視圖要求數據來創建和返回單元格
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 1 {
return buttonTitles.count
} else if collectionView.tag == 2 {
return targetButtonTitles.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
.
.
.
return cell
} else if collectionView.tag == 2 {
let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
cell.buttonOne.setTitle(targetButtonTitles[indexPath.item], for: [])
.
.
.
return cell
}
}
在您的回調中,在您的兩個 arrays 中移動數據,完成后,重新加載兩個集合視圖。
self.targetButtonTitles[indexPath.item] = self.buttonTitles[indexPath.item]
collectionView.reloadData()
targetCollection.reloadData()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.