UITableViewCell背景大小

时间:2010-02-23 17:20:18

标签: iphone uitableview

我正在尝试将背景的大小设置为比默认值短一些,在单元格之间创建一些空间。事实证明这很困难。设置背景视图的框架似乎什么都不做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSString *reuseIdentifier = @"cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (!cell)
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease];

    // Set up the cell...

    cell.contentView.backgroundColor = [UIColor clearColor];

    cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 4, 320, 42)] autorelease];
    cell.backgroundView.backgroundColor = [UIColor blackColor];
    cell.backgroundView.alpha = .2;

    cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 4, 320, 42)] autorelease];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView.alpha = .2;

    cell.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:22.0f];
    cell.selectedTextColor = [UIColor blackColor];
    cell.textColor = [UIColor whiteColor];

    NSDictionary *dict = [files objectAtIndex:indexPath.row];

    cell.text = [dict objectForKey:@"name"];

    return cell;
}

任何帮助?

此外,设置选定的背景视图不会执行任何操作。选择单元格后,背景完全空白。这是为什么?

我正在使用iPhone OS 2.2.1。

我也是这样做的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.rowHeight = 50.0f;
}

您可以在此处下载代码(仅针对此问题制作一个小项目):

http://dl.dropbox.com/u/608462/tabletest2.zip

11 个答案:

答案 0 :(得分:3)

backgroundView不是普通视图,幕后会发生一些事情。检查此链接:

Difference between background view and content view in uitableviewcell

具体来说,来自文档:

backgroundView: 对于普通样式表(UITableViewStylePlain)中的单元格,默认值为nil;对于分组样式表UITableViewStyleGrouped,默认值为非nil。 UITableViewCell将背景视图添加为所有其他视图后面的子视图,并使用其当前帧位置。

因此:它实际上没有帧位置,它使用单元格的帧位置。

此代码有效:

UIImageView *bgView = [[UIImageView alloc] init]; // Creating a view for the background...this seems to be required.
bgView.backgroundColor = [UIColor redColor];
cell.backgroundView = bgView;

UIImageView *bgImageView = [[UIImageView alloc] init]; // Creating a subview for the background...
bgImageView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
[bgImageView setFrame:CGRectInset(cell.bounds, 1, 1)];

[cell.backgroundView addSubview:bgImageView]; // Assigning the subview, and cleanup.
[bgImageView release];
[bgView release];

花了大约一个小时试图解决这个问题......但它确实有效。这是cellForRowAtIndexPath委托方法中的代码 - 我不会在这里覆盖整个事情。

答案 1 :(得分:3)

morgancodes的解决方案使我走向了正确的方向。

我在背景视图中添加了一个子图层并对其进行了样式设置。将背景视图的背景颜色设置为clearColor时,子图层是唯一显示的内容。

UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor clearColor];

CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
sublayer.frame = CGRectMake(15, 3, tableView.frame.size.width - 45, 38);
sublayer.cornerRadius = 5;
[backgroundView.layer addSublayer:sublayer];

cell.selectedBackgroundView = backgroundView;

答案 2 :(得分:2)

这是一种与你正在尝试的完全不同的方法。

我喜欢做的一件事是为backgroundView和selectedBackgroundView使用自定义图像,而不是让iPhone处理着色任务。这使我在细胞渲染方面有了更大的灵活性。只需添加以下内容:

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

要:

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

答案 3 :(得分:2)

另一种方法:在背景中添加子图层。我将以下内容添加到UITableViewCell子类的初始化中,它看起来效果很好。

  UIView* backgroundView = [[UIView alloc] initWithFrame: self.contentView.frame ];

  backgroundView.layer.frame = CGRectInset(backgroundView.layer.frame, 20, 20);

  CALayer *sublayer = [CALayer layer];
  sublayer.backgroundColor = [UIColor colorWithWhite:0.69 alpha:1].CGColor;
  sublayer.frame = CGRectMake(INDENT, 0, width - (INDENT * 2), [ChuckWagonTableViewCellCell cellHeight]) ;
  [backgroundView.layer addSublayer:sublayer];

  self.selectedBackgroundView = backgroundView;

答案 4 :(得分:1)

试试这个:

UIView *bg = [[UIView alloc] initWithFrame: CGRectInset(cell.frame, 0.0, 2.0)];
bg.backgroundColor = [UIColor whiteColor];
cell.backgroundView = bg;

另外不要忘记在viewDidLoad()中设置要清除的背景颜色和分隔符颜色:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
}

答案 5 :(得分:1)

当搞乱背景视图时,我会这样做: - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

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

答案 6 :(得分:0)

尝试使用:

cell.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 4.0, 320.0, 40.0)]];

对于第二个问题,您是否实施了:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

答案 7 :(得分:0)

我认为正在发生的事情是,当您选择一行时,selectbackgroundview的alpha值内部为se,因此显示为完全白色。

答案 8 :(得分:0)

我遇到了类似的问题,但没有一个答案似乎适合我的情况。 在这种情况下,我的所有行都具有相同的高度,但是通过一些数学计算,这可以适应于容纳不同高度的行。

我通过使用UITableViewDelegate方法在控制器中设置了高度。我的控制器上有一个名为cellBackgroundImage的实例变量,UIImage将用于UITableViewCell背景。 UITableView背景设置为[UIColor clearColor]

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return cellBackgroundImage.size.height + SPACING_HEIGHT;
}

其中SPACING_HEIGHT是间隙高度的#define常量。

然后,诀窍是使用UIView来包裹将成为单元格背景的UIImageView。我通过这样做完成了这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContentCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"ContentCell"] autorelease];
        CGFloat height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
        cell.frame = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, height);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *backView = [[UIView alloc] initWithFrame:CGRectInset(cell.frame, 0, 0)];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:cellBackgroundImage];
        [backView insertSubview:imageView atIndex:0];
        cell.backgroundView = backView;
        [backView release];
        [imageView release];
    }

    return cell;
}

然后,通过将cell.backgroundView = backView设置为包含我的背景的UIView的{​​{1}},我设法实现了行之间的间隙效果。 我希望这会有所帮助。

答案 9 :(得分:0)

一个可能的解决方案是子类UIView并添加颜色和高度参数(如果你只想改变高度,否则你可以传递一个size / rect)。请注意,需要设置背景颜色,否则您将看到一个空白区域。

- (id)initWithColor:(UIColor *)color height:(CGFloat)height backgroundColor:(UIColor *)backgroundColor;
{
    self = [super init];

    if (self != nil)
    {
        _color = color;
        _height = height;
        _backgroundColor = backgroundColor;
    }

    return self;
}

添加适当的属性:

@interface CellSelectedBackgroundView ()
@property (strong, nonatomic) UIColor *color;
@property (strong, nonatomic) UIColor *backgroundColor;
@property (assign, nonatomic) CGFloat height;
@end

在drawRect中:你可以填充该区域:

- (void)drawRect:(CGRect)rect
{
    [self.backgroundColor setFill];
    UIRectFill(rect);

    [self.color setFill];
    CGRect frame = CGRectMake(0, 0, self.bounds.size.width, self.height);

    UIRectFill(frame);
}

只需初始化自定义UIView子类并将其设置为UITableViewCell的selectedBackgroundView属性。

答案 10 :(得分:0)

尝试将子视图添加到backgroundViews中,而不是直接修改它们:

UIView *selectedView = [[UIView alloc] initWithFrame:UIEdgeInsetsInsetRect(cell.frame, UIEdgeInsetsMake(8, 8, 8, 8))];
selectedView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
[cell.selectedBackgroundView addSubview:selectedView];

我和selectedBackgroundView有同样的问题,这对我有用;)