UITableViewCell Background Size

I am trying to set my background size a little shorter than the default, creating some space between cells. It turned out to be difficult. Setting the background view frame does nothing:

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

Any help?

In addition, setting the selected background image does nothing. When a cell is selected, the background is completely empty. Why is this?

I am using iPhone OS 2.2.1.

I also do this:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

You can download the code here (a small project made just for this problem):

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

+3
source share
11 answers

The background screen is not a normal view, there is something going on behind the scenes. Check out this link:

uitableviewcell

, :

backgroundView: nil (UITableViewStylePlain) non-nil UITableViewStyleGrouped). 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 - .

+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;
+3

, .

, , - backgroundView selectedBackgroundView, iPhone . , . , , - :

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

To:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
+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;
+2

:

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];
}
+1

, : - (void) tableView: (UITableView *) tableView willDisplayCell: (UITableViewCell *) forRowAtIndexPath: (NSIndexPath *) indexPath {

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

+1

:

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

:

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

, , , , , , - selectedbackview se 1, .

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 UIImageView, , . , .

0

UIView ( , /). , , .

- (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 selectedBackgroundView UITableViewCell.

0

Try adding a subview to your wallpaper instead of changing it directly:

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

I had the same issue as you had with the selected BackgroundView, and it worked for me;)

0
source

Source: https://habr.com/ru/post/1734104/


All Articles