4

当我使用以下代码将重复背景图像设置为 UITableView 时,可以:

tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];

但是当我使用它通过以下代码将重复背景图像设置为 UITableViewCell 时,没有任何反应,我不知道为什么,我也无法为该单元格设置背景颜色。

cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];

有没有人知道这样做的方法?请帮我!

4

2 回答 2

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

这将进入这个代码块:

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

TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
        cell = [[[RecipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-on.png"]];
    }    
// Configure cell

return cell;
}

您还可以看到如何在里面设置选定的图像:selectedBackgroundView。

cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-pressed.png"]];

我不确定是否要重复,但我很确定 backgroundView 可以在其上设置 contentMode。

于 2010-07-13T04:48:25.103 回答
1

哦,这比我想象的要容易。我只需要通过以下代码设置背景图像:

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

并且整个单元格背景视图会自动填充此背景图像。:)

于 2010-07-14T03:41:06.007 回答