swift - 当选择 UIViewController 类的按钮时如何更新 UICollectionViewCell 中标签的颜色

标签 swift uiviewcontroller uicollectionview uilabel uicollectionviewcell

UIView

当选择 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/

相关文章:

swift - 自定义 setter/getter 核心数据的函数

ios - 如果 _ 是 _ 或 .isKind(of : ),正确的使用方法是什么

iphone - 注意 View 是从 viewController 外部加载的

iOS/ObjC : "Fallback" tap gesture recognizer?

ios - 隐藏 CollectionViewCell 而不更新数据源

ios - 禁用滚动时如何确定 UICollectionView 宽度

ios - 从 UICollectionViewCell 实例访问图像时遇到问题

ios - UISplitViewController 中嵌入的 UIViewController 在 iPhone 上不显示栏按钮

Swift、macos - 我可以检测或让其他应用程序位于前台吗?

iphone - UIViewController - 无法成为第一响应者