2
votes

I'm trying to set insets in my tableview so that each cell does not stick to each other.

To do this, I've added the following method to my tableView:willDisplayCell:forRowAtIndexPath:

if([tableView respondsToSelector:@selector(setSeparatorInset:)]){
    [tableView setSeparatorInset:UIEdgeInsetsMake(10, 15, 10, 15)];
}

I basically want a 10pt space on the top and bottom of the cell, before the next cell. This code partially works, because there is a visible 15pt inset on the left and right. However, the top and bottom are still connected.

Thanks!

~Carpetfizz

2

2 Answers

5
votes

The top and bottom values are ignored UITableViewCell Class Reference (separatorInset):

[...]Only the left and right inset values are respected; the top and bottom inset values are ignored.[...]

I'd recommend to do the spacing by increasing the height of the cells and place the content with an appropriate padding to the top and bottom edge.

1
votes

You must use transparent cell for that. A small part of the code for example.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return yourCount*2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"elementCell";
    static NSString *TransparentCellIdentifier = @"transparentCell";

    NSString *neededIdentifier;

    if(indexPath.row % 2 != 0)
        neededIdentifier = TransparentCellIdentifier;
    else
        neededIdentifier = CellIdentifier;

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:neededIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:neededIdentifier];
    }
    if([neededIdentifier isEqualToString:TransparentCellIdentifier]) {
        [cell setBackgroundColor:[UIColor clearColor]];
        cell.userInteractionEnabled = NO;
    }
    else {
        [cell setBackgroundColor:[UIColor redColor]];
        cell.userInteractionEnabled = YES;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row % 2 == 0)
        return 70.0f;
    else
        return 10.0f;
}

Yes, it's a little strange, but it works.