Ios 如何在每次向UITableView添加新的tableViewCell时调用函数
我有一个方法,每次调用它时都返回一种新颜色,我想做的是每次添加一种新颜色时都能用一种新颜色给行着色 使用下面的代码,我只会得到Ios 如何在每次向UITableView添加新的tableViewCell时调用函数,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个方法,每次调用它时都返回一种新颜色,我想做的是每次添加一种新颜色时都能用一种新颜色给行着色 使用下面的代码,我只会得到红色行,而不会调用蓝色、绿色和黄色 同样,我需要做的是能够用数组中四种颜色中的一种给每一行上色,第一行是红色的,第二行是蓝色的,第三行是绿色的,第四行是黄色的,然后第五行是红色的,第六行是蓝色的,依此类推 class CustomCell: UITableViewCell { let myColors = [colorMyRed, colorMyBlue, col
红色
行,而不会调用蓝色、绿色和黄色
同样,我需要做的是能够用数组中四种颜色中的一种给每一行上色,第一行是红色的,第二行是蓝色的,第三行是绿色的,第四行是黄色的,然后第五行是红色的,第六行是蓝色的,依此类推
class CustomCell: UITableViewCell {
let myColors = [colorMyRed, colorMyBlue, colorMyGreen, colorMyYellow]
var nextItemIndex = 0
func color() -> UIColor {
let result = myColors[nextItemIndex]
nextItemIndex = nextItemIndex + 1
return result
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// how can I assign a new color to this variable
// each time awakeFromNib is called
let myColor:UIColor = color()
// these labels need to be colored with one of the
// four colors each time a new row is added
labelPrice.textColor = myColor
labelDiscount.textColor = myColor
labelSavings.textColor = myColor
}
}
有什么建议吗
您可以在UITableViewCell的cellForRowAtIndexPath函数中完成
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cell = tableView.dequeueReusableCellWithIdentifier("CellIndentifier") as UITableViewCell!
// Configure the cell...
switch(indexPath.row) {
case 0, 4, 8, 12:
cell.textLabel!.textColor = UIColor.redColor()
cell.textLabel!.text = "Red"
break
case 1, 5, 9, 13:
cell.textLabel!.textColor = UIColor.blueColor()
cell.textLabel!.text = "Blue"
break
case 2, 6, 10, 14:
cell.textLabel!.textColor = UIColor.greenColor()
cell.textLabel!.text = "Green"
break
case 3, 7, 11, 15 :
cell.textLabel!.textColor = UIColor.yellowColor()
cell.textLabel!.text = "Yellow"
break
default:
cell.textLabel!.textColor = UIColor.blackColor()
cell.textLabel!.text = "Black"
break
}
return cell
}
您可以在UITableViewCell的cellForRowAtIndexPath函数中完成
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cell = tableView.dequeueReusableCellWithIdentifier("CellIndentifier") as UITableViewCell!
// Configure the cell...
switch(indexPath.row) {
case 0, 4, 8, 12:
cell.textLabel!.textColor = UIColor.redColor()
cell.textLabel!.text = "Red"
break
case 1, 5, 9, 13:
cell.textLabel!.textColor = UIColor.blueColor()
cell.textLabel!.text = "Blue"
break
case 2, 6, 10, 14:
cell.textLabel!.textColor = UIColor.greenColor()
cell.textLabel!.text = "Green"
break
case 3, 7, 11, 15 :
cell.textLabel!.textColor = UIColor.yellowColor()
cell.textLabel!.text = "Yellow"
break
default:
cell.textLabel!.textColor = UIColor.blackColor()
cell.textLabel!.text = "Black"
break
}
return cell
}
创建一个充当“颜色分配器”的结构。代码相对简单:
number
变量是currentIndex
模数4,这意味着它将包含一个介于0和3之间的值currentIndex
属性。这确保下次调用此方法时,您将获得一个新的number
结果getNewColor
方法将给出一种新颜色
例如,您可以在当前代码中这样使用:
override func awakeFromNib() {
super.awakeFromNib()
let myColor = Colors.getNewColor()
labelPrice.textColor = myColor
labelDiscount.textColor = myColor
labelSavings.textColor = myColor
}
创建一个充当“颜色分配器”的结构。代码相对简单:
number
变量是currentIndex
模数4,这意味着它将包含一个介于0和3之间的值currentIndex
属性。这确保下次调用此方法时,您将获得一个新的number
结果getNewColor
方法将给出一种新颜色
例如,您可以在当前代码中这样使用:
override func awakeFromNib() {
super.awakeFromNib()
let myColor = Colors.getNewColor()
labelPrice.textColor = myColor
labelDiscount.textColor = myColor
labelSavings.textColor = myColor
}
下面是我是如何做到这一点的。谢谢大家的帮助 CustomCell.swift
class CustomCell: UITableViewCell {
var labelColor: UIColor! {
get {
return labelPrice.textColor
}
set {
labelPrice.textColor = newValue
labelDiscount.textColor = newValue
labelSavings.textColor = newValue
}
}
}
ViewController.swif(***cellforrowatinexpath*)**
下面是我是如何做到这一点的。谢谢大家的帮助 CustomCell.swift
class CustomCell: UITableViewCell {
var labelColor: UIColor! {
get {
return labelPrice.textColor
}
set {
labelPrice.textColor = newValue
labelDiscount.textColor = newValue
labelSavings.textColor = newValue
}
}
}
ViewController.swif(***cellforrowatinexpath*)**
nextItemIndex
是一个实例变量。每次创建一个新单元格时,它都会被初始化为零。您需要将其设置为静态以使其工作。我认为在Swift static中,它的工作方式与在其他语言中的工作方式不同。当然,Swift的语法非常糟糕,但是static
的工作原理基本相同:谢谢您的帮助,但是最后,我重新构造了代码,以便在CellForRowatineXpath
中进行着色。谢谢大家。这是最好的aproac,因为tableView对单元格进行着色时不会调用awakeFromNib。因此,最好在CellForRowatineXpathnextItemIndex
中保持计数,这是一个实例变量。每次都会创建一个新的单元格创建后,它被初始化为零。您需要将其设置为静态以使其工作。我认为在Swift中,静态的工作方式与在其他语言中的工作方式不同。当然,Swift的语法很糟糕,但是static
的工作方式基本相同:谢谢您的帮助,但我最终重新构建了代码,以便在cellForR中进行着色OwatineXpath
成功了。谢谢大家。这是最好的aproac,因为tableView对单元格进行查询时不会调用awakeFromNib。因此最好在CellForRowatineXpath中保持计数