Table View Cell Separator Lines Not Showing

Discussion in 'iOS Programming' started by RagingGoat, May 19, 2013.

  1. RagingGoat macrumors 6502

    Joined:
    Jun 21, 2010
    #1
    I've set the image for the cells in my table view but the lines dividing the cells aren't showing. What have I done wrong?

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *mbTableIdentifier = @"SimpleTableItem";
        UIImageView *image = [[UIImageView alloc]init];
        image.image = [UIImage imageNamed:@"BarButton.png"];
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mbTableIdentifier];
    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mbTableIdentifier];
            cell.textLabel.font=[UIFont systemFontOfSize:16.0];
        }
    
        // cell.backgroundView = [[CustomCellBackground alloc] init];
        cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundView = image;
    
        cell.textLabel.text = [mbTableData objectAtIndex:indexPath.row];
        return cell;
    }
    
    I can see the separator lines if I un-comment
    Code:
    cell.backgroundView = [[CustomCellBackground alloc] init];
    
    I also logged the separator style and color

    2013-05-19 21:38:21.103 KFBNewsroom[2593:c07] cell separator style: 2 separator color: UIDeviceRGBColorSpace 0.67 0.67 0.67 1
    2013-05-19 21:38:21.104 KFBNewsroom[2593:c07] cell separator style: 2 separator color: UIDeviceRGBColorSpace 0.67 0.67 0.67 1
    2013-05-19 21:38:21.105 KFBNewsroom[2593:c07] cell separator style: 2 separator color: UIDeviceRGBColorSpace 0.67 0.67 0.67 1
     

    Attached Files:

  2. blueillusion macrumors member

    Joined:
    Aug 18, 2008
    #2
    Hm, perhaps post the code to CustomCellbackground?

    Also, when setting a background view for a cell, you should normally not do it in cellForRowAtIndexPath, but rather in the willDisplayCell:ForRowAtIndexPath: method. There you should setup the cell colour/background view.
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    Background color, yes.

    However, you should not add structural elements like background views to a cell in willDisplayCell. That gets called each time a table view is getting ready to display a cell.

    CellForRowAtIndexPath gets called when cells are created/recycled. (Under the new approach in storyboards your cell prototypes build the custom cells, but that's another story)
     
  4. blueillusion macrumors member

    Joined:
    Aug 18, 2008
    #4
    Ah yes, I just doubled checked the method documentation, and you are absolutely right about that. Made me double check some of my code in my programs haha.
     
  5. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #5
    As I understand it...
    By setting your view as the background view you are removing the background view and the drawing it does. So the cell separators doesn't get drawn.

    Try adding your view to the background view.
     
  6. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #6
    Code:
    cell.backgroundView = image;
    
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    What is this single line of code, without any explanation, supposed to tell us?
     
  8. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #8
    I ended up adding a line to the bottom of my image.
     
  9. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #9
    Consider the difference between your line and this one.
    Note: Assuming "image" is a valid UIView or subclass.
    Code:
    [cell.backgroundView addSubview:image]; 
    
     

Share This Page