当选择 TextField 下方的 Button 时,如何更改顶部 UICollectionViewCell 内标签的颜色(例如 Q1)?
我只想当前问题的标签更改其文本颜色 即,如果用户正在处理问题 1,则选择该按钮时,会将 Q1 颜色从橙色更改为白色。
请参阅上图
我使用 UIViewController 作为 UICollectionView 的委托(delegate)/数据源
最佳答案
更新 UICollectionViewCell
/UITableViewCell
最好通过调用
collectionView.reloadItems(at: [IndexPath])
我想您有一系列问题和一些正确/错误的标记。所以你应该有按钮选择处理程序和单元格组合方法
@objc private func onSomeButton(_ sender: UIButton) {
let questionNumber: Int = 999
self.markAnswer(correct: true, atIndex: questionNumber)
self.collectionView.reloadItems(at: [IndexPath(item: questionNumber, section: 0)])
}
然后这个调用将触发重新加载这个单元格
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.someDefaultRequseId, for: IndexPath)
cell.label.textColor = self.isAnswerCorrect(at: indexPath.item) ? .white : .red
}
关于swift - 当选择 UIViewController 类的按钮时如何更新 UICollectionViewCell 中标签的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51162781/