ITeye博客

UItableview cell 背景

夜鸣猪 · 2013-03-20 11:01 · 编程语言 · Markdown




Here is the most efficient way I have come across to solve this problem, use the willDisplayCell delegate method (this takes care of the white color for the text label background as well when using cell.textLabel.text and/or cell.detailTextLabel.text):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }

When this delegate method is called the color of the cell is controlled via the cell rather than the table view, as when you use:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }

So within the body of the cell delegate method add the following code to alternate colors of cells or just use the function call to make all the cells of the table the same color.

if (indexPath.row % 2)
{
    [cell setbackgroundcolor:[uicolor colorwithred:.8 green:.8 blue:1 alpha:1]];
}
else [cell setbackgroundcolor:[uicolor clearcolor]];

This solution worked well in my circumstance...



static NSColor *SelectedCellBGColor = ...;
static NSColor *NotSelectedCellBGColor = ...;

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *currentSelectedIndexPath = [tableview indexpathforselectedrow];
    if (currentSelectedIndexPath != nil)
    {
        [[tableview cellforrowatindexpath:currentselectedindexpath] setBackgroundColor:NotSelectedCellBGColor];
    }

    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableview cellforrowatindexpath:indexpath] setBackgroundColor:SelectedCellBGColor];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell.isSelected == YES)
    {
        [cell setbackgroundcolor:selectedcellbgcolor];
    }
    else
    {
        [cell setbackgroundcolor:setbackgroundcolor:notselectedcellbgcolor];
    }
}

评论(1)

  1. #1 楼 · witcheryne · 2013-03-21 17:14

    试试 editStyle, editStyle 中支持 multiSelect 看看.

    
    UIView *backgrdView = [[uiview alloc] initWithFrame:cell.frame];
    backgrdView.backgroundColor = [uicolor whitecolor];
    cell.backgroundView = backgrdView;
    
    

评论功能维护中,当前仅展示历史评论。