开发者

UITableViewCell background image and selection problem

开发者 https://www.devze.com 2023-01-13 04:17 出处:网络
It\'s my first iphone app, and i have trouble with styling my tableView. I have two images (png), one for standard cell state, and one for selected state.

It's my first iphone app, and i have trouble with styling my tableView.

I have two images (png), one for standard cell state, and one for selected state.

In my subclassed cell, I tried the following :

1) setting up front the backgroundView and selectedBackgroundView

UIImage *ib = [UIImage imageNamed:@"tab.png"];
UIImageView *back = [[UIImageView alloc] initWithImage:ib];
self.backgroundView = back;
[back release];

UIImage *is = [UIImage imageNamed:@"selected_tab.png"];
UIImageView *selected = [[UIImageView alloc] initWithImage:is]; 
self.selectedBackgroundView = selected;
[selected release];

The standard cell is fine, but when selected, the two images are shown.

2) just playing with background view on selection :

// storing the 2 uiviews in class attributes
UIImage *ib = [UIImage imageNamed:@"tab.png"];
UIImageView *back = [[UIImageView alloc] initWithImage:ib];
self.storedStandard = back;
[back release];
UIImage *is = [UIImage imageNamed:@"selected_tab.png"];
UIImageView *selected = [[UIImageView alloc] initWithImage:is]; 
self.storedSelected = selected;
[selected release];

- (void)setSelec开发者_如何学Pythonted:(BOOL)selected animated:(BOOL)animated {
    if (selected) {
        self.backgroundView = self.storedSelected;
    }
    else {
        self.backgroundView = self.storedStandard;
    }

}

It nearly works, but the images don't fit the cell, i don't know to stretch them to the size of the cell.

For me, the first solution should have been the one, based on the properties' names, and the second solution seems like a hack (like 90% of the tutorials i seen btw), so i'm a bit frustrated.

To sum up : why the first one doesn't work, and how could i force images to take all the cell space ?

Thanks a lot :)


if you have a UITableViewDelegate, you should be able to just change the backgroundView's image in the willSelectRowAtIndexPath: method and then change it back in the didSelectRowAtIndexPath: method. This is a bit of a hack however.

0

精彩评论

暂无评论...
验证码 换一张
取 消