选择 UIViewController Class 的按钮时如何更新 UICollectionViewCell 中的标签颜色
How to update Colour of Label in UICollectionViewCell when UIViewController Class's button is selected
UIView
How do I change the Colour of the label inside the UICollectionViewCell at the top (e.g. Q1) when Button below the TextField is selected?
我只希望当前问题的标签更改其文本颜色
i.e. if user is on Question 1, when the button is selected it changes the Q1 Colour from Orange to White.
请看上图
我使用 UIViewController 作为 UICollectionView
的 delegate/datasource
更新 UICollectionViewCell
/UITableViewCell
最好通过调用
collectionView.reloadItems(at: [IndexPath])
我想你有一系列问题和一些 correct/wrong 标志。所以你应该有按钮选择处理程序和单元格组合方法
@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
}
如果我对你的情况的理解正确,你不需要在你的集合视图中滚动,因此你不需要单元格的可重用性。所以你需要一个静态结构。对于这种情况,最好对静态单元格使用 UITableView
。您可以为每个单元格以及单元格内的每个元素设置 IBOutlet
到主视图控制器中。然后在 TouchUpInside
特定按钮的操作中为标签设置颜色。
UIView
How do I change the Colour of the label inside the UICollectionViewCell at the top (e.g. Q1) when Button below the TextField is selected?
我只希望当前问题的标签更改其文本颜色 i.e. if user is on Question 1, when the button is selected it changes the Q1 Colour from Orange to White.
请看上图
我使用 UIViewController 作为 UICollectionView
的 delegate/datasource更新 UICollectionViewCell
/UITableViewCell
最好通过调用
collectionView.reloadItems(at: [IndexPath])
我想你有一系列问题和一些 correct/wrong 标志。所以你应该有按钮选择处理程序和单元格组合方法
@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
}
如果我对你的情况的理解正确,你不需要在你的集合视图中滚动,因此你不需要单元格的可重用性。所以你需要一个静态结构。对于这种情况,最好对静态单元格使用 UITableView
。您可以为每个单元格以及单元格内的每个元素设置 IBOutlet
到主视图控制器中。然后在 TouchUpInside
特定按钮的操作中为标签设置颜色。