简体   繁体   中英

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]];
    }
}

Try this.

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
    }
}

Use below code.

- (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++;
    }

If you mean cell's background color, the simplest way to change it:

cell.backgroundView.backgroundColor = [UIColor greenColor];

It looks like:

- (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;
}


write below code

- (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's answer worked for me, although I changed the order of colors used to white as the second color as seen below for what I thought was a better UI. I also used a uicolor generator at www.UIColor.org to find just the right color for my needs. Hopes this helps.

  • (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 it's alternating the color of cell then you can use
if(indexPath.row % 2 == 0) { cell.contentView.backgroundColor = [UIColor grayColor]; } else cell.backgroundColor = [UIColor brownColor];

and if the colors are random ie first one is gray, second and third is brown and forth one white etc. etc. then you should make an array of UIColor objects, like

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

and then in the method

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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