Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hi all,

I'm playing around with tables. And because I wanted the cells have some 'padding' between them, I had 2 options.

-Make every row their own 'section' which then can have an defines header and footer size.
-Make the tableviewcell with an 'own' view inside, which has a backgroundcolor, and the actuall cell will have the backgroundcolor clearColor.

For less coding I used the 2nd option.
Since when I'm going to select an row, it will always be row 1 etc etc. There is a lot of coding needed to get it to work.

But now for selecting the cells, when I'm setting a color for selectedBackgroundView, it will set the color for the actuall cell and ofcourse not the view inside which I'm using as the cell with padding.

I tried to connect the variable selectedBackgroundView to the view which I'm using, but then I don't get anything in my tableview :p

Is there some kind of workaround?
I also tried to hardcode the frame of selectedBackgroundView, but the frame dit not change at all...

Any help will be appriciated!

p.s. I'm working with 1 section in my table and as you can guess its a custom cell with its own .xib
 
Don't change the cell backgroundView color change the cell.ownView.backgroundColor you'll have to change it back to the original color too.
 
Don't change the cell backgroundView color change the cell.ownView.backgroundColor you'll have to change it back to the original color too.

Thanks for your reply, however it's a little unclear.
Before I did this:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
myItem *cell = [self.devicesTable dequeueReusableCellWithIdentifier: @"myCell"];
    if(!cell) {
        [tableView registerNib: [UINib nibWithNibName: @"myCell" bundle: nil] forCellReuseIdentifier: @"myCellIdent"];
        cell = [tableView dequeueReusableCellWithIdentifier: @"myCellIdent
    }

UIView *customColorView = [[UIView alloc] init];
    customColorView.backgroundColor = [UIColor colorWithHexString: @"CECECE"];
    cell.selectedBackgroundView = customColorView;

    return cell;
}

For now I'm already back to sections instead of rows, and did the simple code above again.

But now I'm about to do other stuff to highlight instead of the whole cell.

Do I have to do this with
Code:
didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
and
didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
?
Do I have to do this with
Code:
didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
and
didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath

But how? Even the full background does not change by :
Code:
[[tableView cellForRowAtIndexPath: indexPath] setBackgroundColor: [UIColor redColor]];
 
I'm not really sure what you're doing but willDisplayCell:forRowAtIndexPath: is often used to adjust the appearance of a cell just before it's shown.
 
I'm not really sure what you're doing but willDisplayCell:forRowAtIndexPath: is often used to adjust the appearance of a cell just before it's shown.

Actually I'm trying to override the selected behaviour. The cell is already shown, am I right? Or is it going through the loop again when selecting?
You know, when you press and hold on an cell, by default it becomes blue.
By :
Code:
customColorView.backgroundColor = [UIColor colorWithHexString: @"CECECE"];
    cell.selectedBackgroundView = customColorView;

I override it to be really light grey.

But that's just the basic selected color.
Now I would like to change a color of a text when I'm selecting a cell.
Or more specific, I'd like to start an animation like, rotating a star (image) when selecting a cell.

I'm not sure if this is possible, but that's what I'm looking for.
 
UITableViewCell has setSelected and setSelected:animated: methods. You should override one or both of those, and do what you like when the selected state changes.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.