1

Please go through these points

  • Hi I have added a tableview dynamically to my code.
  • I want to show my own seperator in default tableview cell.
  • As default seperator has at some distance from y-axis.

I know the answer that we can use cell seperator inset property to achieve this. But the problem I have encountered something strange.

DropDownTbl=[[UITableView alloc]initWithFrame:CGRectMake(BookTypeTxt.frame.origin.x, BookTypeTxt.frame.origin.y+BookTypeTxt.frame.size.height, BookTypeTxt.frame.size.width-11, height)];
DropDownTbl.separatorStyle = UITableViewCellSeparatorStyleNone;
DropDownTbl.tag=98;
DropDownTbl.delegate=self;
DropDownTbl.dataSource=self;

[self.settingView addSubview:DropDownTbl];
[DropDownTbl reloadData];

And in my tableview cell I have added seperator in this style , problem is in this way seperator view gets added twice as displayed in the given image

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.textLabel.text=[BookTypeArray objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,cell.contentView.frame.size.height-1, DropDownTbl.frame.size.width, 1)];
lineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:lineView];

cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];

return cell;

enter image description here

So I decide to add the linview in cell == nil parenthesis

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,cell.contentView.frame.size.height-1, DropDownTbl.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    [cell.contentView addSubview:lineView];
}

cell.textLabel.text=[BookTypeArray objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f];
cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];

return cell;

In this way my seperator gets disappeared when ever cell is reused. I just want to know why this happens?

Denis
  • 492
  • 2
  • 15
Himan Dhawan
  • 894
  • 5
  • 23

1 Answers1

1

This issue is generate because cellForRowAt called multiple times when you scroll your TableView everytime this code add new line in cell contentView.

Try this.

[[cell.contentView viewWithTag:5] removeFromSuperview];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,cell.contentView.frame.size.height-1, tableView.frame.size.width, 1)];
lineView.tag = 5;
lineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:lineView];
Denis
  • 492
  • 2
  • 15