【问题标题】:Change color of alternate row in UITableView for iPhone SDK更改 iPhone SDK 的 UITableView 中交替行的颜色
【发布时间】:2013-03-19 11:01:05
【问题描述】:

在我的应用程序中,我想在 iPad 中显示多列表视图。

所以,我使用了来自 :MultiColumn TableView 的可可控件

它对我来说很好,但我想以交替颜色显示行。

为此,我无法找到更改代码的位置。

【问题讨论】:

  • 备用行 ??哪一行请详细说明你的问题..
  • 在 cellForRowAtIndexPath 中你可以放那个代码。

标签: ios objective-c uitableview


【解决方案1】:
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
 
        if(indexPath.row % 2 == 0)
              cell.backgroundColor = [UIColor redColor];
        else
              cell.backgroundColor = [UIColor whiteColor];
}

【讨论】:

  • 请注意,此解决方案不适用于带有部分的表。你会想要表格的 absolute 行,这可以在我实现的这个类别中找到:github.com/edekhayser/UITableView-RowConvenience
  • 解决方案完美!!,但奇怪的是,如果你不写else部分,滚动后它将不起作用!!!,不应该table view为else部分采用默认颜色。
【解决方案2】:

使用cellForRowAtIndexPath 中的indexPath 来获得所需的结果。

    if( [indexPath row] % 2){
          cell.backgroundColor=[UIColor whiteColor];
    } 
    else{
          cell.backgroundColor=[UIColor purpleColor];
    }

你将在实现UITableViewDelegate的类中拥有这个委托方法

【讨论】:

  • 感谢您的回复!里面有很多文件,所以我找不到更改代码的方法。如果你曾经使用过这个控件,那么请告诉我我将在其中更改代码的文件名。
  • 您需要在实现UITableViewDelegate的类中设置它
【解决方案3】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .......

    if (indexPath.row % 2 == 0) {
       cell.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    .......

    return cell;
}

【讨论】:

    【解决方案4】:

    首先你需要对每一行取模。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        .......
    
        if (indexPath.row % 2 == 0) {
           cell.backgroundColor = [UIColor lightGrayColor];
        }
        else
        {
            cell.backgroundColor = [UIColor darkGrayColor];
        }
        ....
    
    
        return cell;
    }
    

    【讨论】:

      【解决方案5】:

      任何尝试快速执行此操作的人都是我的代码。小巧玲珑。

      这个技巧甚至适用于我不使用 cellForRowAtIndexPath 方法的静态单元格。

      抱歉,回答很少。希望您了解数据源和委托。

       override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
          
          if (indexPath.row % 2) != 0{
              cell.backgroundColor = UIColor .blackColor()
          }else{
              cell.backgroundColor = UIColor .lightGrayColor()
      
          }
      
       }
      

      它会给你这样的结果......

      【讨论】:

        【解决方案6】:
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
         
        static NSString *CellIdentifier = @"Cell";
            
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }
            if(indexPath.row%2==0)
                cell.contentView. backgroundColor=[UIColor redColor];
            else
                cell.contentView.backgroundColor=[UIColor greenColor];
        
                
        
        
        return cell;
        
        
        }
        

        【讨论】:

        • 感谢您的回复!里面有很多文件,所以我找不到更改代码的方法。如果你曾经使用过这个控件,那么请告诉我我将在其中更改代码的文件名。
        【解决方案7】:

        在您的代码中搜索 cellForRowAtIndexPath。该方法负责在您的表格视图中创建单元格。 欲了解更多信息:UITableViewCell with alternate background color in customized cells

        【讨论】:

          【解决方案8】:
          func colorForIndex(index: Int) -> UIColor 
          {
              let itemCount = stnRepos.count - 1
              let color = (CGFloat(index) / CGFloat(itemCount)) * 0.6
              return UIColor(red: 0.80, green: color, blue: 0.0, alpha: 1.0)
          }
          
          func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
              forRowAtIndexPath indexPath: NSIndexPath) 
          {        
              if (indexPath.row % 2 == 0)
              {
                  cell.backgroundColor = colorForIndex(indexPath.row)
              } else {
                  cell.backgroundColor = UIColor.whiteColor()()
              }
          }
          

          【讨论】:

            【解决方案9】:

            对于 Swift 3 + 并降低不透明度:

            func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            
                    if(indexPath.row % 2 == 0) {
                        cell.backgroundColor = UIColor.red.withAlphaComponent(0.05)
                    } else {
                        cell.backgroundColor = UIColor.white
                    }
            }
            

            【讨论】:

              【解决方案10】:

              在这样一个简单的例子中,我会选择三元运算符。我只是讨厌看到 cell.backgroundColor 重复。

              func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              
              .
              .
              .
                  cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.red.withAlphaComponent(0.05) : .white
              .
              .
              .
              }
              

              【讨论】:

                【解决方案11】:

                在您的 cellForRow 方法中,您应该检查 indexPath.row 是否可被 2 整除分配第一种颜色,否则分配第二种颜色并返回结果单元格

                【讨论】:

                  【解决方案12】:

                  这是 Swift 4.x 及更高版本的工作代码

                  func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                      if(indexPath.row % 2 == 0) {
                          cell.backgroundColor = UIColor.white
                      } else {
                          cell.backgroundColor =  UIColor.lightGray
                      }
                  }
                  

                  【讨论】:

                    【解决方案13】:

                    您可以运行一个循环,它将索引 path.row 增加 2,然后在该循​​环的主体中您可以分配颜色。,

                    【讨论】:

                      猜你喜欢
                      • 2019-03-28
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-05-08
                      • 1970-01-01
                      • 2011-02-03
                      • 2016-09-30
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多