zac*_*161 5 colors cell uitableview visual-glitch swift
我正在制作一个使用彩色表格视图单元格将其他单元格分成类别的应用程序。我通过使用 if else 语句为单元格着色不同的颜色来做到这一点。但出于某种原因,当我启动应用程序时,我在表格视图上上下滚动得越多,其他单元格随机改变颜色的次数也就越多。这是我的自定义 instrumentTableCell 类中的代码:
@IBOutlet var nameLabel: UILabel?
@IBOutlet var descriptionLabel: UILabel?
@IBOutlet var thumbnailImage: UIImageView!
func configurateTheCell(recipie: Recipie) {
self.nameLabel?.text = recipie.name
self.descriptionLabel?.text = recipie.description
self.thumbnailImage?.image = UIImage(named: recipie.thumbnails)
if nameLabel!.text == "Instrument"
{
backgroundColor = UIColor.orangeColor()
nameLabel?.textColor = UIColor.blackColor()
}
else if nameLabel!.text == "Optional addon"
{
backgroundColor = UIColor.cyanColor()
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if nameLabel!.text == "Instrument"
{
return 20
}
else if nameLabel!.text == "Optional addon"
{
return 20
}
else
{
return 100
}
}
Run Code Online (Sandbox Code Playgroud)
此外,如果有人知道如何,我希望彩色单元格也更小,以便应用程序看起来更好。
您可以设置 cellForRowAtIndexPath 委托方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("your identifier", forIndexPath: indexPath)
if cell.recipie.name == "Instrument"
{
backgroundColor = UIColor.orangeColor()
nameLabel?.textColor = UIColor.blackColor()
}
else if cell.recipie.name == "Optional addon"
{
backgroundColor = UIColor.cyanColor()
}
else{
backgroundColor = UIColor.whiteColor()
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1414 次 |
| 最近记录: |