swift - 如何为表格视图单元格和扩展单元格生成相同的随机颜色?
问题描述
我正在创建一个 iOS 应用程序,当我单击表格视图行时,该行会扩展到其下方的另一行。我想在每次单击行时为表视图行背景生成随机颜色,并且我希望展开的行与单击的行颜色相同。每次单击该行时,我都想生成不同的随机颜色。我怎么做?
我尝试将 cellForRowAt 的代码放在不同的位置,但似乎没有任何效果。
func generateRandomColor() -> UIColor {
let redValue = CGFloat.random(in: 0...1)
let greenValue = CGFloat.random(in: 0...1)
let blueValue = CGFloat.random(in: 0...1)
let randomColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
return randomColor
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dataIndex = indexPath.row - 1
guard let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1") else {return UITableViewCell()}
var sectionBackgroundColor: UIColor! = tableViewData[indexPath.section].color
if indexPath.row == 0 {
if tableViewData[indexPath.section].opened == false {
tableViewData[indexPath.section].opened = false
cell1.textLabel?.text = tableViewData[indexPath.section].title
cell1.textLabel?.numberOfLines = 0
cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell1.textLabel?.font = UIFont.systemFont(ofSize: 18)
cell1.backgroundColor = UIColor.clear
return cell1
}
else if tableViewData[indexPath.section].opened == true {
tableViewData[indexPath.section].opened = true
cell1.textLabel?.text = tableViewData[indexPath.section].title
cell1.textLabel?.numberOfLines = 0
cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell1.textLabel?.font = UIFont.boldSystemFont(ofSize: 25)
if sectionBackgroundColor == nil {
sectionBackgroundColor = generateRandomColor()
tableViewData[indexPath.section].color = sectionBackgroundColor
}
cell1.backgroundColor = sectionBackgroundColor
return cell1
}
return cell1
} else {
cell1.textLabel?.text = tableViewData[indexPath.section].sectionData[dataIndex]
cell1.textLabel?.numberOfLines = 0
cell1.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell1.textLabel?.font = UIFont.systemFont(ofSize: 18)
if sectionBackgroundColor == nil {
sectionBackgroundColor = generateRandomColor()
tableViewData[indexPath.section].color = sectionBackgroundColor
cell1.backgroundColor = sectionBackgroundColor
}
else if sectionBackgroundColor != nil {
sectionBackgroundColor = generateRandomColor()
tableViewData[indexPath.section].color = sectionBackgroundColor
cell1.backgroundColor = sectionBackgroundColor
}
cell1.backgroundColor = sectionBackgroundColor
return cell1
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 {
if tableViewData[indexPath.section].opened == true {
tableViewData[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
} else {
tableViewData[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
}
}
我希望单击表格视图行,并且该行在单击的行下扩展为新行,并且每次单击该行时,单击的行和扩展的行都会生成相同的随机颜色,但实际输出是当我单击该行时,该行在单击的行下方扩展为一行并生成随机颜色,但两行并不总是生成相同的颜色。
解决方案
推荐阅读
- logstash - Kibana 监控选项卡 - 看不到所有 Logstash 节点
- java - OSGi 版本注释的奇怪 javadoc 错误(使用 jdk12)
- jquery - 在 jQuery 中循环遍历 json
- c# - 如何让 RibbonComboBox 与 ICollectionView.CurrentItem 同步
- .net - UWP AdaptiveTrigger minWindowWidth 背景没有改变
- javascript - sequelize 包含 sum 使用时,在操场上重复显示相同的数据
- swift - 如何在使用 ARFaceTrackingConfiguration 时将 SceneNode 始终定位在场景视图的中心
- sql - SQL Server 2012 中的级联 While 循环
- visual-studio - 如果可能,定义规则以使成员私有
- heroku - 为什么我无法连接到数据库?