繁体   English   中英

更改iPhone应用程序中的每个UITableViewCell背景颜色?

[英]Change each UITableViewCell background color in iPhone app?

我正在使用UITableView为iPhone应用程序工作。 在我的表格视图中,我有多个包含多个单元格的部分。 我想为每个单元格提供单元格背景色。 这意味着each cell should have different colors

例如:如果有20个单元格,则意味着每20个单元格应具有不同的单元格背景色。

您能否告诉我是否可以为每个单元格提供单元格背景颜色/图像? 你能指导我做到这一点吗?

提前致谢。

编辑

我的Tableviewcell颜色将如下所示,

First Cell background color : white color
Second Cell background color : red color
Third Cell background color : orange color
Fourth cell background color : gray color
Fifth Cell background color : white color
Sixth cell background color : white color
Seventh cell background color : white color
Eight cell background color : gray color
Ninth cell background color : red color
Tenth cell background color : red color

我想给这样的tableview单元格背景色。 然后,当UITableView重新加载或新打开时,背景颜色将发生变化。 你能帮我这样做吗? 可以在iPhone应用程序中进行tableview吗?

提前致谢。

实际上,您实际上不能使用标准的tableView:cellForRowAtIndexPath:工作流程来执行此操作。 单元格的背景色是一种特殊情况,因为表格视图将在管理单元格的选定状态时对其进行修改。

相反,如本答案所述,您必须实现tableView:willDisplayCell:atIndexPath:方法。 我想在2009年或2010年会议上有一个旧的WWDC视频,涵盖了此内容。

例如,您可以执行以下操作:

- (void)tableView:(UITableView*)tableView 
  willDisplayCell:(UITableViewCell*)cell 
forRowAtIndexPath:(NSIndexPath*)indexPath
{
    cell.backgroundColor = /* random color here */;
}

尝试这个

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

    UITableViewCell *cell = [tTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    double numSections = [self numberOfSectionsInTableView:tableView];
    double numRows = [self tableView:tableView numberOfRowsInSection:indexPath.section];
    [cell setBackgroundColor:[UIColor colorWithRed:0.8/numRows*((double)indexPath.row)+0.2 green:0.8/numSections*((double)indexPath.section)+0.2 blue:((double)(random()%1000))/1000.0 alpha:1]];
    return cell;
}

这有点复杂,但是这里是在cellForRowAtIndexPath中执行此操作的方法。 只需将此代码粘贴到您的方法中即可。 我是根据您上面的说明写的。

if (indexPath.row == 0 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6) {
            [cell.textLabel setBackgroundColor:[UIColor whiteColor]];
            cell.contentView.backgroundColor = [UIColor whiteColor];
            [cell.textLabel setTextColor:[UIColor blackColor]];
        } else {
            if (indexPath.row == 1 || indexPath.row == 8 || indexPath.row == 9){
                 [cell.textLabel setBackgroundColor:[UIColor redColor]];
                 cell.contentView.backgroundColor = [UIColor redColor];
                 [cell.textLabel setTextColor:[UIColor whiteColor]];
            } else {
                  if (indexPath.row == 3 || indexPath.row == 7){
                 [cell.textLabel setBackgroundColor:[UIColor grayColor]];
                 cell.contentView.backgroundColor = [UIColor grayColor];
                 [cell.textLabel setTextColor:[UIColor whiteColor]];
                 } else {
                      [cell.textLabel setBackgroundColor:[UIColor orangeColor]];
                      cell.contentView.backgroundColor = [UIColor orangeColor];
                      [cell.textLabel setTextColor:[UIColor blackColor]];
}}}

当然,还有其他方法可以做到这一点(例如,使用switch或通过声明的UIColor并根据调用的indexPath.row更改其值)。 但是在实施之后,您可以自己简化它。

请注意,如果要保持均匀性,则必须设置textLabel和contentView的背景色。

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM