我在里面有按鈕Collection View Cell每當一個人點擊那個按鈕時,我想注冊一個通知,創建變數name并將其值設定為單元格的屬性nameLabel,之后我想將該通知發送到我的第二個Collection View并將該Collection View單元格的nameLabel.text 設定為name我首先發送的字串Collection View
如果您需要任何附加代碼,請在評論中告訴我
extension Notification.Name {
static let AddToFavorites = Notification.Name("add_to_favorites")
}
// button inside first collection view cell
@IBAction func likeButoon(_ sender: Any) {
print("Button works fine")
let name = self.nameLabel.text // first collection view cell's property nameLabel
NotificationCenter.default.post(name: .AddToFavorites, object: name)
}
// second collection view controller
var string1:String = ""
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(notificationRecieved), name: .AddToFavorites, object: nil)
}
@objc func notificationRecieved(notification: Notification){
guard let name = notification.object as? String else {
return
}
string1 = name
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionViewFavorites.dequeueReusableCell(withReuseIdentifier: "favoritesCell", for: indexPath) as? CollectionViewCellFavorites
cell?.nameLabel.text = string1 // Second collection view cell's property nameLabel
return cell!
}
uj5u.com熱心網友回復:
你需要重繪
string1 = name
collectionView.reloadData()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322514.html
