简体   繁体   English

我将如何以编程方式更改单元格的背景颜色

[英]How would I programmatically change the background color of a cell

So my actual question is a bit more robust. 所以我的实际问题更加强大。 I'm curious whether it's possible to change the background color of a cell programmatically, however it would be based on whether the cell was first, or second, etc. 我很好奇是否可以通过编程方式更改单元格的背景颜色,但这可能是基于单元格是第一个还是第二个等等。

I'm not sure this is even possible, but what I'm trying to achieve is a gradient effect utilizing the cells as they increase in number. 我不确定这是否可行,但我想要实现的是利用细胞的梯度效应,因为它们的数量增加。

if (indexPath.row % 2)
    {
        cell.contentView.backgroundColor = UIColor.redColor()
    }
    else
    {
        cell.contentView.backgroundColor = UIColor.blueColor()
    }

I've tried something like this to at least have the color alternate to see if I can figure out what to do next, but this has failed. 我尝试过这样的事情,至少让颜色交替出现,看看我能否弄清楚接下来要做什么,但这已经失败了。 Alternating doesn't seem to be the right option, but it might be the right way to start programming what I want to happen. 交替似乎不是正确的选择,但它可能是开始编程我想要发生的正确方法。

A tableView sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. tableView在使用单元格绘制行之前将此消息发送到其委托,从而允许委托在显示单元格对象之前对其进行自定义。 for more info 了解更多信息

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row % 2 == 0 {
         cell.backgroundColor = UIColor.redColor()
    }
    else {
         cell.backgroundColor = UIColor.blueColor()
    }
    return cell
}

Yes, it is possible to change the background color of a cell programmatically, based on whether the cell was first, or second, etc. In cellForRowAtIndexPath delegate method, check if row is odd then put the different background color, if it is even then put the different color. 是的,可以根据单元格是第一个还是第二个等以编程方式更改单元格的背景颜色。在cellForRowAtIndexPath委托方法中,检查行是否为奇数然后放入不同的背景颜色,如果它是偶数把不同的颜色。 By using below code, you will get alternate colors of rows. 通过使用下面的代码,您将获得行的替代颜色。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentTableCellIdentifier"];
    cell.textLabel.text = @"Your text goes here";
    if(indexPath.row % 2 == 0)
        cell.backgroundColor = [UIColor redColor];
    else
        cell.backgroundColor = [UIColor greenColor];
    return cell;
}

SWIFT:- 迅速:-

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell

    cell.textLabel.text = "Your text goes here"
    if(indexPath.row % 2 == 0)
    {
        cell.backgroundColor = UIColor.redColor()
    }
    else
    {
        cell.backgroundColor = UIColor.greenColor()
    }
    return cell
}

Swift 4.2 Swift 4.2

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if indexPath.row % 2 == 0 {

         cell.backgroundColor = UIColor.red
         //OR
         productCell.contentView.backgroundColor = UIColor.red
    }
    else {

         cell.backgroundColor = UIColor.blue
    }

    return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改警报视图的背景颜色 - How would I change background color of alert view 如何更改具有多个部分的表格单元格的背景颜色? - How can i change the background color of a table cell with multiple sections? 如何更改UItableview单元格accesoryType的背景​​颜色? - How to change the background color of the UItableview cell accesoryType? 如何在 iPhone sdk 中以编程方式更改键盘的背景颜色? - How to change the background color of the keyboard programmatically in iPhone sdk? 如何使用单元格内的UIButton更改CustomCell背景颜色 - How can I change my CustomCell background color with an in-cell UIButton 如何在Swift iOS中更改自定义“收藏夹视图”单元格的背景颜色? - How to change the background color of a custom Collection View cell in swift iOS? 如何更改所选单元格的背景颜色而不会丢失文本 - How to change the selected cell background color without missing the text 推后如何更改所选单元格的背景颜色? - How to change the background color of the selected cell after pushing? 如何更改选定的可扩展表格视图单元格的背景颜色 - How to change the background color of selected expandable table view cell 如何以编程方式设置UILabel的背景颜色? - How can I programmatically set the background color of a UILabel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM