UITableView顶部和底部分隔线插图无法工作

3
我正在尝试设置我的表格视图的插入,以使每个单元格不会粘在一起。

为此,我已经将以下方法添加到我的tableView:willDisplayCell:forRowAtIndexPath中:

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

我想在单元格顶部和底部留出10pt的空间,以便下一个单元格。这段代码部分有效,因为左右有可见的15pt插入。但是,顶部和底部仍然连接在一起。

谢谢!

~Carpetfizz

2个回答

5
“顶部和底部值被忽略”UITableViewCell Class Reference (separatorInset)中指出:
只有左和右的内边距值才会生效。顶部和底部的内边距值将被忽略。
我建议通过增加单元格的高度并使用适当的上下内边距来调整间距。

1

你必须使用透明单元格来实现这个功能。以下是一个示例的部分代码。

- (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;
}

是的,这有点奇怪,但它有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接