UITableView 单元格背景,如“音乐”应用程序 [重复]

Posted

技术标签:

【中文标题】UITableView 单元格背景,如“音乐”应用程序 [重复]【英文标题】:UITableView Cell background like in 'Music' app [duplicate] 【发布时间】:2012-07-17 21:44:02 【问题描述】:

可能重复:Setting background color of a table view cell on iPhonehow to change the color alternate cell on table view

我想要一个具有正常背景的单元格,而下一个具有一定背景的单元格,如下图所示: 关于如何为无限数量的单元实现这种漂亮和干净的任何想法? 提前致谢!

【问题讨论】:

那是一个“伟大”的背景......你试过什么?这些怎么样:***.com/questions/10213364/…***.com/questions/900094/…***.com/questions/281515/… 第二个几乎就是你的问题。其实这基本上是重复的。 【参考方案1】:

FizzBu​​zz:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    

    if ((indexPath.row % 2) == 0) 
        [cell.contentView setBackgroundColor:[UIColor whiteColor]];
        [cell.textLabel setBackgroundColor:[UIColor whiteColor]];
    
    else 
        [cell.contentView setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]];
        [cell.textLabel setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]];
    
    cell.textLabel.text = [songArray objectAtIndex:indexPath.row];
    return cell;

【讨论】:

这种方法很好,除非您在单元格中有任何编辑附件。我试过的这段代码[cell.accessoryView setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]]; 不起作用

以上是关于UITableView 单元格背景,如“音乐”应用程序 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

分组 UITableView 上的自定义背景图像

IOS uitableview单元格选中背景

iOS7.0中UITableView单元格背景的区别

uitableview 单元格背景的问题

uitableview 单元格的背景

移出可见的 uitableView 后,UITableViewCell 具有灰色背景