代码之家  ›  专栏  ›  技术社区  ›  quano

UITableViewCell背景大小

  •  3
  • quano  · 技术社区  · 14 年前

    我正在尝试将背景的大小设置为比默认值稍短一点,在单元格之间创建一些空间。事实证明这很困难。设置背景视图的框架似乎没有任何作用:

    - (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;
    }
    

    有什么帮助吗?

    我用的是iPhoneOS2.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 回复  |  直到 14 年前
        1
  •  3
  •   Community kfsone    7 年前

    背景不是一个正常的看法,有一些事情在幕后。查看此链接:

    Difference between background view and content view in 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委托方法中的代码——我在这里显然不会讨论全部内容。

        2
  •  3
  •   stafffan    12 年前

    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;
    
        3
  •  2
  •   Rob Jones    14 年前

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

    我喜欢做的一件事是为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;
    
        4
  •  2
  •   morgancodes    12 年前

    还有一种方法:在背景中添加一个子层。我在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;
    
        5
  •  1
  •   sha    14 年前

    试试这个:

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

    另外,不要忘记在viewdiload()中将背景色和分隔符颜色设置为清除:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tableView.separatorColor = [UIColor clearColor];
        self.tableView.backgroundColor = [UIColor clearColor];
    }
    
        6
  •  1
  •   Scott Pfeil    13 年前

    在处理背景视图时,我会在:

    而不是: -(UITableViewCell*)tableView:(UITableView*)tableView单元格ForRowAtIndexPath:(NSIndexPath*)indexPath{

        7
  •  0
  •   Rob Jones    14 年前

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

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    
        8
  •  0
  •   Ghar    14 年前

        9
  •  0
  •   cvbarros    13 年前

    我有一个类似的问题,而且似乎没有一个答案适合我的情况。 在这个例子中,我所有的行都有相同的高度,但是通过一些数学运算,这可以适应不同高度的行。

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

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

    其中,间距#高度为#定义间隙高度的常数。

    然后,诀窍是使用 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公司 UIImageView 包含了我的背景,我设法实现了行之间的间隔效果。 我希望这有帮助。

        10
  •  0
  •   JanApotheker    10 年前

    一种可能的解决方案是将UIView子类化,并添加color和height参数(如果只想更改高度,否则可以传递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);
    }
    

        11
  •  0
  •   inigo333    9 年前

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

    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];
    

    我在所选的背景视图上遇到了与您相同的问题,这对我很有效;)