在我的应用程序中,我单击一行,它会在其下方展开另一行。我想生成一种随机颜色,当我点击一行时,行背景会变成该颜色,而它下面的展开行会变成相同的颜色。如何让行生成相同的随机颜色?
我创建了一个生成随机颜色的函数,当isOpened设置为true时我调用了行的函数单元格,并且我为扩展的单元格调用了相同的函数。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dataIndex = indexPath.row - 1
guard let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1") else {return UITableViewCell()}
func generateRandomColor() -> UIColor {
let redValue = CGFloat(drand48())
let greenValue = CGFloat(drand48())
let blueValue = CGFloat(drand48())
let randomColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
return randomColor
}
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)
cell1.backgroundColor = generateRandomColor()
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)
cell1.backgroundColor = generateRandomColor()
return cell1
}
}
生成随机颜色,但展开的行生成新颜色。
您需要存储生成的随机颜色。每次调用generateRandomColor()时,都会生成一个新的。我要做的是创建一个行到颜色的字典。
var color: [Int: UIColor?] = [:]
然后在cellForRowAtIndexPath中确定您是否已经有该行的颜色,如果没有计算一个颜色:
if let color = colors[indexPath.row] {
cell.backgroundColor = color
} else {
colors[indexPath.row] = generateRandomColor()
}
然后在didSelectRowForIndexPath中,您可以使用以下命令检索randomColor:
if let color = colors[indexPath.row] {
// Expand the cell with the new color
}
在本地func之后插入:let _color = generate Random Color()可以赋值此值,必须具有相同颜色的元素。
修改您的section dataSource以包含颜色,如下所示:
struct SectionData {
let title: String
var color: UIColor?
}
在if indexPath.row == 0 {
线之前,添加以下内容
var backgroundColor: UIColor! = self.tableViewData[indexPath.section].color
if backgroundColor == nil {
backgroundColor = generateRandomColor()
self.tableViewData[indexPath.section].color = backgroundColor
}
cell1.backgroundColor = backgroundColor
现在删除其他行cell1.backgroundColor = generateRandomColor()
我已经尝试了所有这些方法,我只发现了一个按照我想要的方式工作的方法。
我不得不修改我的dataSource:
struct cellData {
var opened = Bool()
var title = String()
var sectionData = [String]()
var color: UIColor? = UIColor()
}
var tableViewData = [cellData]()
func generateRandomColor() -> UIColor {
let redValue = CGFloat(drand48())
let greenValue = CGFloat(drand48())
let blueValue = CGFloat(drand48())
let randomColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1.0)
return randomColor
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dataIndex = indexPath.row - 1
guard let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1") else {return UITableViewCell()}
var backgroundColor: UIColor! = tableViewData[indexPath.section].color
if backgroundColor == nil {
backgroundColor = generateRandomColor()
tableViewData[indexPath.section].color = backgroundColor
}
cell1.backgroundColor = backgroundColor
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)
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)
return cell1
}
}