繁体   English   中英

如何更改表格视图上的颜色备用单元格

[英]how to change the color alternate cell on table view

我想在数据库中的表格视图单元格上显示数据,所以我想根据第一个单元格灰色的数据平均值为单元格提供颜色,然后再次将下两个单元格设置为棕色我想显示灰色单元格,所以谁能告诉我什么是最简单的程序吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row % 2 == 0) {
        [cell setBackgroundColor:[UIColor whiteColor]];
    }
    else {
        [cell setBackgroundColor:[UIColor lightGrayColor]];
    }
}

尝试这个。

if(indexPath.row % 2 == 0)
{
[cell setBackgroundColor:[UIColor grayColor]];
}   
else
[cell setBackgroundColor:[UIColor brownColor]];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2==0) 
    {
         cell.contentView.backgroundColor = [UIColor grayColor];  // first color (greyColor or grayColor can't remember spelling)
    }
    else 
    {
         cell.contentView.backgroundColor = [UIColor brownColor];  // second color
    }
}

使用下面的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static int alternateColor = 0;

        if((alternateColor % 2) == 0 ) 
        {
             cell.contentView.backgroundColor = [UIColor whiteColor];  
        }
        else 
        {
             cell.contentView.backgroundColor = [UIColor blackColor];
        }
       alternateColor++;
    }

如果您的意思是单元格的背景颜色,最简单的更改方法是:

cell.backgroundView.backgroundColor = [UIColor greenColor];

看起来像:

- (UITableViewCell *) tableView:tableView cellForRowAtIndexPath:indexPath
{
    cell = ... // dequeue or initialize cell
    switch (criteriaFromDatabase) 
    {            
        case 0:
            cell.backgroundView.backgroundColor = [UIColor greenColor];
            break;
        case 1:
            cell.backgroundView.backgroundColor = [UIColor blueColor];
            break;
        default:
            cell.backgroundView.backgroundColor = [UIColor redColor];
            break;
    }
    return cell;
}


写下面的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2==0) 
    {
        //Write your code for even rows like 0,2,4
    }
    else 
    {
         //write your code for odd rows like 1,3,5
    }
}

Mitesh Khatri 的回答对我有用,尽管我将颜色的顺序更改为白色作为第二种颜色,如下所示,我认为这是一个更好的用户界面。 我还使用了 www.UIColor.org 上的 uicolor 生成器来找到适合我需要的颜色。 希望这会有所帮助。

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

     if (indexPath.row % 2 == 0) { [cell setBackgroundColor:[UIColor colorWithRed:81.0/255.0 green:102.0/255.0 blue:145.0/255.0 alpha:0.1]]; } else { [cell setBackgroundColor:[UIColor whiteColor]];

Priyanka,如果它改变了细胞的颜色,那么你可以使用
if(indexPath.row % 2 == 0) { cell.contentView.backgroundColor = [UIColor grayColor]; } else cell.backgroundColor = [UIColor brownColor];

如果颜色是随机的,即第一个是灰色,第二个和第三个是棕色,第四个是白色等等,那么你应该制作一个 UIColor 对象数组,比如

NSArray *arryColor = [NSArray arrayWithObject:[UIColor grayColor],[UIColor brownColor],[UIColor  brownColor],[UIColor  whiteColor],[UIColor  redColor],[UIColor  greenColor],nil];

然后在方法中

- (UITableViewCell *) tableView:tableView cellForRowAtIndexPath:indexPath
    {
        // your cell creating stuff


        cell.contentView.backgroundColor = [arryColor objectAtIndex:indexPath.row];
        return cell;
    }

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM