Aijaz Ali
Aijaz Ali

Reputation: 403

UITableView separator line does not show in iPhone app

I have app in which i am using tableView problem is that when tableView has one record then it does not show separator line but shows when there are two records.

The first cell in tableView is textField. here is the code i am using.

    for (UIView *subView in cell.subviews)
    {
    if (subView.tag == 2 || subView.tag == 22) 
    {
        [subView removeFromSuperview];
    }
    }

   tableView.backgroundView=nil;

   tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

   tableView.separatorInset = UIEdgeInsetsZero;


   if(indexPath.section==0){

   tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(0,0,248,31)];



    tagInputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    tagInputField.textAlignment=UITextAlignmentLeft;
    tagInputField.backgroundColor=[UIColor whiteColor];

    tagInputField.tag = 2;
    tagInputField.delegate = self;
    tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;


    [tagInputField.layer setCornerRadius:5.0f];
    [tagInputField.layer setMasksToBounds:YES];
    tagInputField.layer.borderWidth = 0.3;
    tagInputField.layer.borderColor = [UIColor darkGrayColor].CGColor;


    tagInputField.font=[UIFont fontWithName:@"Myriad-Pro" size:8];
    [tagInputField setText:@"Enter tag here "];
    tagInputField.textColor =[UIColor grayColor];





    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    [cell addSubview:tagInputField];






    return cell;



}


if(indexPath.section==1) {
    UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(228, 8, 18, 18)];
    crossButton.tag = 22; //use a tag value that is not used for any other subview
    //crossButton.backgroundColor = [UIColor purpleColor];
    crossButton.backgroundColor  = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Cross.png"]];
    [cell addSubview:crossButton];
    cell.textLabel.font =[UIFont fontWithName:@"Myriad-Pro" size:8];
    cell.textLabel.textColor =[UIColor grayColor];

    cell.backgroundColor=[UIColor whiteColor];

    cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
    [crossButton addTarget:self action:@selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];


    [tagInputField setFrame:CGRectMake(5,0,248,31)];


    tableView.backgroundColor=[UIColor whiteColor];


    [tagInputField.layer setCornerRadius:0.0f];
    [tagInputField.layer setMasksToBounds:YES];


    tagInputField.layer.borderWidth = 0.0;
    tagInputField.layer.borderColor = [UIColor clearColor].CGColor;




    return cell;
}

Upvotes: 3

Views: 6592

Answers (5)

sam_smith
sam_smith

Reputation: 6093

I found none of the above answers worked. I was particularly wary of the answer suggesting adding a UIView to look like the line.

In the end I found that this answer worked for me:

tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;

In another answer it was suggested that adding worked better

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;

but I found that this was unnecessary.

All the credit goes to user859045 and samvermette for their answers on different threads. samvermette's thread especially has a lot of answers on this topic which are very helpful and worth a look.

Upvotes: 0

Sourabh Sharma
Sourabh Sharma

Reputation: 8322

I used this to add last cell separator line....

  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
        {
            return 1.0f;
        }

        - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
        {
            // To "clear" the footer view
            UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
            separatorLine.layer.borderColor = [UIColor grayColor].CGColor;
            separatorLine.layer.borderWidth = 1.0;
            return separatorLine;
        }

Upvotes: 0

Jeremie D
Jeremie D

Reputation: 4204

add an empty footer at the end of the tableview to show the separator. To do so implement the following tableview delegate method in your class with your other tableview delegate functions:

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.001; }

Upvotes: 0

Ganapathy
Ganapathy

Reputation: 4614

Just try like this in side the Cell_for_Row_At_Index_path delegate method:

[tableView setSeparatorInset:UIEdgeInsetsZero];
    [tableView setSeparatorColor:[UIColor greenColor]];

just paste this line and check.

Upvotes: 1

Retro
Retro

Reputation: 4005

When you will have only 1 record then separator will not show, and you should setting the separator for tableView at viewDidLoad look like

- (void)viewDidLoad
{
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

and in any case you want to show your own separator for every single cell ten try to add a imageView or somrthing witch share by the table cell

UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
separatorView.layer.borderColor = [UIColor redColor].CGColor;
separatorView.layer.borderWidth = 1.0;
[cell.contentView addSubview:separatorView];

How to customize tableView separator in iPhone

go for more on it....

Upvotes: 5

Related Questions