【问题标题】:UITableViewCell customization - change backgroundUITableViewCell 自定义 - 改变背景
【发布时间】:2011-09-19 17:02:40
【问题描述】:

我确信这是一个已经被问及并得到解答的问题,但我似乎找不到我需要的东西。我正在尝试自定义 UITableView 的背景,为此,我创建了 UITableViewCell 的子类,并使用 IB 构建了自定义单元格。我成功地将自定义单元格连接到表格视图,所以我得到的单元格正在显示。我正在尝试更改每个单元格的背景,所以这是我使用的:

if(row == 1){
    rowBackground = [UIImage imageNamed:@"VehicleExpenses_button.png"];
    selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"];
    ((UIImageView *)cell.backgroundView).image = rowBackground;
  //  ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}

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

我的 Controller 类的方法。我检查了调试器,代码中的所有行都被调用了......

有什么想法吗?

这是整个方法:

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

    UIImage *rowBackground;
    UIImage *selectionBackground;

    NSInteger row = [indexPath row];


    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }

    if(row == 1){

        rowBackground = [UIImage imageNamed:@"VehicleExpenses_button.png"];
        selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"];
        ((UIImageView *)cell.backgroundView).image = rowBackground;
      //  ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
    }

    // perform additional custom work...

    return cell;
}

【问题讨论】:

    标签: iphone uitableview xcode4


    【解决方案1】:

    也许我错了,但我看不到您将 backgroundView 初始化为 UIImageView 的位置。这是我要添加到创建单元格的行中的内容:

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
            cell = (UITableViewCell *)[nib objectAtIndex:0];
            cell.backgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
            cell.selectedBackgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
        }
    

    注意:将“cellHeight”替换为您想要的高度。

    我希望这会有所帮助。

    【讨论】:

    • 我尝试在上面执行此代码,但没有成功...我会丢失任何可能的连接吗?
    • 我不确定这是否相关,但是每次我访问 tableView 时,例如在 tableView.bounds.size.width 中,我都会收到一条警告,上面写着“'tableView' 的本地声明隐藏实例变量'
    • @Heather:这意味着您将 .h 文件中的 tableview 对象称为“tableView”。这是一个问题,因为该方法有一个具有确切名称的局部变量......编译器不知道您指的是哪个。
    • @Heather:不知道为什么图片没有显示...你确定 png 图片在项目文件夹中吗?您可以编辑单元格的其他属性(例如,设置一些文本以查看它是否呈现)?我注意到您正在尝试在 row==1 上设置背景...这是第二行...这就是您要找的吗?只是想这里的一切。
    • 我删除了 .h 文件中的 tableView 实例。我已将它从 IB 连接到我拥有的 UITableView,因此我可以设置行高。我尝试重新运行代码,第二行仍然没有改变。
    【解决方案2】:

    你可以用这个:

    UIImageView *cellBackGround = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectedRow.png"]];
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundView:cellBackGround];
    [cellBackGround release];
    

    【讨论】:

      【解决方案3】:

      小问题。不要强制转换和设置backgroundViewselectedBackgroundView 的图像属性,而是将它们设置为从图像创建的UIImageView

      cell.backgroundView = [[[UIImageView alloc] initWithImage:rowBackground] autorelease];
      cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:selectionBackground] autorelease];
      

      【讨论】:

      • 我刚试过这个,还是没有运气。我应该在视图控制器中更改单元格的背景,而不是自定义 UITableViewCell 类,对吗?
      • @Heather:是的,更改控制器中单元格的背景。您在示例中尝试的是正确/流行的方法……请参阅我的答案;它可能会有所帮助。
      猜你喜欢
      • 2011-08-05
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      相关资源
      最近更新 更多