簡體   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