代码之家  ›  专栏  ›  技术社区  ›  RonLugge

如何根据空UITableViewCell对象的位置修改其背景色?

  •  5
  • RonLugge  · 技术社区  · 15 年前

    我正在尝试编写代码,根据单元格在表中的位置修改其背景色。虽然下面的代码“有效”,但它只影响传递给它的单元格。空单元格不会生效。

    - (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
    {
        if(!indexPath.row%2)
        {
            cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] ;
            NSLog(@"Blue");
        }
        else{
            cell.backgroundColor = [UIColor whiteColor];
            NSLog(@"white");
        }
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    }
    

    当列表中只有很少的项目时,如果为空,如何影响显示的其余单元格?

    4 回复  |  直到 13 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    但它会影响所有的空细胞。

    也可以更改分隔符类型。

    另外,请看这里: http://www.zenbrains.com/blog/en/2010/06/color-de-fondo-de-celdas-vacias/

    编辑:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // 7 depends on how many cell is shown
        return ([source count] <= 7) ? 7 : [source count];
    }
    

    发件人: Show blank UITableViewCells in custom UITableView

        2
  •  1
  •   iCode    13 年前

    如果设置了单元格contentView的backgroundColor属性,而不是单元格的backgroundColor属性,则它应该可以工作:

    cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] ;
    
        3
  •  0
  •   Brandon    15 年前

    你应该实施

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

        4
  •  0
  •   Julian F. Weinert    11 年前

    [[cell contentView] setBackgroundColor:[UIColor colorWithRed:0 green:128/255.f blue:0 alpha:1]];
    

    in方法

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

    与willDisplayCell方法中使用的逻辑相同

    正如这里所建议的: http://www.zenbrains.com/blog/en/2010/06/color-de-fondo-de-celdas-vacias/

    推荐文章