gpt4 book ai didi

ios - Cell 中只有第一个 UILabel,更新了 UICollectionView - Swift

转载 作者:可可西里 更新时间:2023-11-01 01:43:54 30 4
gpt4 key购买 nike

我正在尝试更新存在于我的 UICollectionView 单元格上的 UILabel。虽然我遇到的问题是只有第一个单元格用新字符串更新

我首先尝试将 IBOutlet 连接到 Cell 中的 UILabel,但是遇到了这个问题:

Main.storyboard: error: Illegal Configuration: Connection "name" cannot have a prototype object as its destination.

接下来,我尝试使用标签 - 但是使用这种方法,只有一个 UICollectionView 单元格得到更新。

// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}

override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return 1000
}

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell

var nameLbl : UILabel? = self.collectionView.viewWithTag(100) as? UILabel;

nameLbl?.text = "woof woof"

return cell
}

我怎样才能更新所有单元格?我将来这个标签会改变,所以它需要是动态的

我曾经在 Objective-C 中使用:

UILabel *label = (UILabel*)[cell.contentView viewWithTag:LABEL_TAG];

最佳答案

Objective-C 声明的 Swift 对应物:

UILabel *label = (UILabel*)[cell.contentView viewWithTag:LABEL_TAG];

是:

let label = cell.contentView.viewWithTag(LABEL_TAG) as UILabel

所以替换:

var nameLbl : UILabel? = self.collectionView.viewWithTag(100) as? UILabel;

与:

let nameLbl = cell.contentView.viewWithTag(100) as UILabel
//or
//let nameLbl = cell.viewWithTag(100) as UILabel

然后你就可以写:

nameLbl.text = "woof woof"

关于ios - Cell 中只有第一个 UILabel,更新了 UICollectionView - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25461261/

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