如何在tableview中的两个单元格之间留出空间?

Ank*_*han 49 iphone cocoa-touch objective-c uitableview ios4

我希望在表视图中的两个单元格之间有一个空格,

我想要这样的细胞,

在此输入图像描述

我怎样才能做到这一点?

Bri*_*ian 121

您不能直接设置单元格之间的距离,但您可以在截面中设置标题的高度以获得相同的结果.

1.设置您需要的单元格数量作为部分:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 3; // in your case, there are 3 cells
}
Run Code Online (Sandbox Code Playgroud)

2.每个部分只返回1个细胞

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

3.在节中设置标题的高度以设置单元格之间的空间

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.; // you can have your own choice, of course
}
Run Code Online (Sandbox Code Playgroud)

4.设置标题的背景颜色以清除颜色,因此它看起来不会很奇怪

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
Run Code Online (Sandbox Code Playgroud)


oba*_*aid 25

在TableView中获取两个单元格之间空间的最佳方法,以这种方式在numberofsections的委托方法中声明所需的节数

例如,您有10个对象的数组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return [array count]; //array count returns 10
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return 1;// this should be one because it will create space between two cells if you want space between 4 cells you can modify it.
}
Run Code Online (Sandbox Code Playgroud)

那么重要的一点是在cellForRowAtIndexPath委托方法中你需要使用indexpath.section而不是indexpath.row

cell.textLabel.text=[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.section]];
Run Code Online (Sandbox Code Playgroud)

那就是检查你的tableview中两个单元格之间的空间.请享用!


DSh*_*hah 22

你也可以在UITableView中创建一个TableView的部分...这个方法是强制性的,所以创建部分,在每个部分你可以像你的图片一样创建单个单元格.

  • 我不知道为什么这个答案被接受了 (11认同)

ave*_*dev 16

多个部分的答案可行,但它非常脆弱,并且不允许实际部分.相反,您应该创建一个自定义单元格,或者在底部和/或顶部只有间隙的自定义单元格原型.

使用IB中的支柱和弹簧来保持均匀的间隙,并使用heightForRowAtIndexPath返回包含间隙的高度.


Pra*_*til 13

目标 - C.

 UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];/// change size as you need.       
 separatorLineView.backgroundColor = [UIColor whiteColor];// you can also put image here
 [cell.contentView addSubview:separatorLineView];
Run Code Online (Sandbox Code Playgroud)

Swift 3(这同样适用于Swift 4)

var separatorLineView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 3))
/// change size as you need.       
separatorLineView.backgroundColor = UIColor.white
// you can also put image here
cell.contentView.addSubview(separatorLineView)
Run Code Online (Sandbox Code Playgroud)

它对我有用.


Adn*_*nan 10

如果有人在寻找Swift版本.干得好.

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 10; // space b/w cells
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return items.count // count of items
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = UIView()
    header.backgroundColor = UIColor.clearColor()
    return header
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
Run Code Online (Sandbox Code Playgroud)

  • 我建议执行header.userInteractionEnabled = false,以便清除标题视图不会接收到触摸.如果视图与按钮重叠. (2认同)

Kha*_*ari 6

对于那些寻找在不使用剖面的情况下显示单元格之间间隙的替代方法的人,您可能希望显示如下所示的替代颜色和高度.使用clearColor的间距.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    if (indexPath.row % 2 == 1)
    {
        static NSString *CellIdentifier = @"cellID1";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                            reuseIdentifier:CellIdentifier];
        }

        cell.backgroundColor = [UIColor colorWhite];

        return cell;

    } else {

        static NSString *CellIdentifier2 = @"cellID2";
        UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell2 == nil) {
            cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                            reuseIdentifier:CellIdentifier2];
        }
        cell2.backgroundColor = [UIColor clearColor];

        return cell2;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 1) {
        return 40.0;
    } else {
        return 2.0;
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*rei 5

有时,您可能真的希望将 tableview 分成几行,并有 1 个部分。例如,如果您需要为该表视图显示自定义标题,该标题在您滚动浏览该部分时保持原位,则可能会发生这种情况。

在这种情况下,我建议做的是返回一个比单元格正常高度更大的浮点数:

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

然后确保表格样式为普通样式,并且单元格分隔符为无。您可以在 XIB 文件本身或代码中执行此操作:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.style = UITableViewStylePlain;

也许还可以将单元格的选择样式添加到无(否则看起来您选择的不仅仅是单元格的可见部分)。

cell.selectionStyle = UITableViewCellSelectionStyleNone;

这会给单元格之间留下空间的印象,但同时将它们作为一行保留在一个部分中(有时这正是您想要的)。


Abd*_*eed 5

我为此使用了一种简单快捷的方法(使用故事板)

  • 在控制器中添加 UITableView
  • 将分隔符样式设置为无(来自属性检查器)
  • 将行的高度设置为距所需高度的顶部和底部各 5 pts
  • 现在在单元格中添加一个图像,从左侧和右侧固定它但留下 5 pts 空间(用于填充感觉)并将图像的背景设置为与您想要的单元格背景相同

加载表格视图时,会感觉单元格之间有空格。


PAC*_*PAC 5

cellForRowAtIndexPath UITableViewDelegate在返回单元格之前,在方法中添加这些行.

let separator = UIView(frame: CGRectMake(0, 0, cell!.bounds.size.width, 1))
separator.backgroundColor = UIColor.whiteColor()
cell.contentView.addSubview(separator)
Run Code Online (Sandbox Code Playgroud)