Erik
Erik

Reputation: 2530

Adding a separator to UITableViewCell

I'm build an app (iOS 8.0) whereas I've got a UITableViewControlled built using AutoLayout and Storyboards. The tableView is set up as static & grouped, and the tableView separator is set to none.

I'm trying to use this code (called in viewWillAppear:) to add a separator to two other cells, but for some reason, it doesn't show:

        // Create a separator for the tableView cells
    UIView *separatorViewTop = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 1.0f, self.fromCell.frame.size.width, 1.0f)];
    UIView *separatorViewBottom = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 43.0f, self.fromCell.frame.size.width, 1.0f)];
    separatorViewTop.backgroundColor = [UIColor grayColor];
    separatorViewBottom.backgroundColor = [UIColor grayColor];

    // Add it
    [self.fromCell.contentView addSubview:separatorViewTop];
    [self.toCell.contentView addSubview:separatorViewBottom];

fromCell and toCell and connected in IB with IBOutlets.

Could please someone take a look at it? Thanks!

Upvotes: 0

Views: 139

Answers (2)

Niko
Niko

Reputation: 3422

You can also create a UIView with 1px height, and with background color of your separator you want to be

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    //Your code to create the tableviewcell...
    //Below 'theCell' is the current cell at index path to return

    if (thisIsTheCellIWantToAddSeparator) {
        UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, theCell.frame.size.height-1, theCell.frame.size.width, 1)];
        theCell.backgroundColor = YOURCOLOR; //Set the color you want
        [theCell.contentView addSubview:imgView];
    }
    return  theCell;
}

Upvotes: 0

Nitheesh George
Nitheesh George

Reputation: 1367

if your are using XIB for Custom cell, then it would be easy to add these separator views in XIB itself. I think it is more flexible as compare to adding at run time.

Upvotes: 1

Related Questions