【问题标题】:Setting background image of custom table view cells设置自定义表格视图单元格的背景图像
【发布时间】:2010-02-16 07:36:07
【问题描述】:

我尝试了多种方法来设置未选中表格单元格的背景图像,但都没有成功:

1- In IB setting the image field
2- cell.contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"list.png"]];
3- cell.imageView.image = [UIImage imageNamed:@"list_selected.png"];

似乎一切都失败了。选定单元格的图像设置有效,但不适用于未选定的单元格。有人知道这里可能出了什么问题吗?

谢谢

【问题讨论】:

    标签: iphone cocoa-touch image uitableview


    【解决方案1】:

    尝试将 backgroundView 设置为图像的 imageView。

    来自 Jason Moore 的代码示例(带有 TomH 的更正):

    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]] autorelease];
    

    【讨论】:

    • cell.backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
    • 我相信上面的内容会泄露——最后添加一个自动释放
    • 不应该是cell.backgroundView = [[UIImageView alloc] initWithImage:.... UITableViewCell 没有backgroundImage 属性
    【解决方案2】:

    cell.backgroundImage 不再存在。而是使用:

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
    

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html

    【讨论】:

      【解决方案3】:

      我一直在使用以下 UITableViewDelegate 方法执行此操作,并设置 cell.backgroundColor 属性。它最后被调用,就在单元格实际绘制在屏幕上之前:

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
      if (indexPath.row%2)
          cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableCell-BG-Dark.png"]];
      else 
          cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableCell-BG-Light.png"]];
      

      }

      是的,我正在使用 UITableViewCell 的自定义子类。

      【讨论】:

        【解决方案4】:

        尝试这样做:

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DateCellIdentifier];
                if (cell == nil) {
                    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:DateCellIdentifier] autorelease]
                    UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gradient.png"]];
                    [cell setBackgroundView:img];
                    [img release];
                } 
                cell.textLabel.text = @"Text";
        }
        

        【讨论】:

        • 感谢您的回复,高拉夫!我正在为表格视图使用自定义构建的单元格。我按原样尝试了您的上述代码,并将背景图像设置代码放在自定义单元控制器类的构造函数中,但仍然无效。
        【解决方案5】:

        我在尝试更改单元格的背景颜色时也遇到了问题,我最终出于不同的原因对单元格进行了子类化,这是我的单元格背景交替代码:

        if (indexPath.row % 2 == 0) {
            cell.contentView.backgroundColor = [UIColor colorWithWhite:230.0/255 alpha:1];
        } else {
            cell.contentView.backgroundColor = [UIColor colorWithWhite:242.0/255 alpha:1];
        }
        

        【讨论】:

        • 请注意,这似乎仅适用于自定义单元格,而不是库存单元格。在股票单元格中,背景仍然适用,但所有标签的背景都设置为白色:\
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-14
        • 2011-02-23
        • 2023-03-24
        • 1970-01-01
        • 2010-10-28
        相关资源
        最近更新 更多