UITableView cells selectedBackgroundView

Discussion in 'iOS Programming' started by DennisBlah, Apr 5, 2015.

  1. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    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
     
  2. Punkjumper macrumors member

    Joined:
    Jan 12, 2013
    #2
    Don't change the cell backgroundView color change the cell.ownView.backgroundColor you'll have to change it back to the original color too.
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    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]];
    
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    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.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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.
     

Share This Page