【问题标题】:how to change the color alternate cell on table view如何更改表格视图上的颜色交替单元格
【发布时间】:2011-08-22 04:24:04
【问题描述】:

我想在数据库中的表格视图单元格上显示数据,所以我想根据第一个单元格灰色的数据手段为单元格赋予颜色,然后再接下来的两个单元格为棕色我想显示灰色单元格, 那么谁能告诉我最简单的程序是什么?

【问题讨论】:

    标签: iphone


    【解决方案1】:

    Mitesh Khatri 的回答对我有用,尽管我将使用的颜色顺序更改为白色作为 second 颜色如下所示,我认为这是一个更好的 UI。我还在 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]];
      

    【讨论】:

      【解决方案2】:
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          if (indexPath.row % 2 == 0) {
              [cell setBackgroundColor:[UIColor whiteColor]];
          }
          else {
              [cell setBackgroundColor:[UIColor lightGrayColor]];
          }
      }
      

      【讨论】:

        【解决方案3】:

        使用下面的代码。

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            【解决方案5】:

            试试这个。

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

            【讨论】:

              【解决方案6】:
              - (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
                  }
              }
              

              【讨论】:

                【解决方案7】:


                写下代码

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

                【讨论】:

                  【解决方案8】:

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

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

                  【讨论】:

                  • “给单元格颜色根据数据(来自数据库)” - 没有理由反对。 “第一个单元格灰色,然后接下来的两个单元格为棕色,我想显示灰色单元格” - 仅作为示例
                  猜你喜欢
                  • 1970-01-01
                  • 2011-04-27
                  • 2018-10-04
                  • 2022-12-25
                  • 1970-01-01
                  • 2015-01-24
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多