在iOS7上保留UITableView标题的填充,但不要在分隔符中添加填充。

27
随着iOS7的更新,表格分隔符左边有15像素的内边距。我知道可以通过UITableView设置中的分隔符插入特性来去除这个内边距,但我需要保留具有内边距的标题文本。如何做到这一点?
默认情况下如第一张图片所示: enter image description here 通过将分隔符插入自定义为0,可以实现如第二张图片所示的效果: enter image description here 我需要保留类似第一张图片中“2013”标题的分隔符样式。

你的tableview中只有一个section还是有多个? - Rashad
不,这是针对我所有收到通知的日子。 - Alessio Crestani
7个回答

51

使用 Swift 和 Storyboard

在Storyboard中,您可以为TableView(标题)和单元格(分隔符)设置不同的分隔符插入。

通过在Storyboard中选择单元格并设置以下内容,将单元格的分隔符插入设置为0:

enter image description here

然后通过在Storyboard中选择TableView并设置以下内容,将TableView(标题)的分隔符插入设置为15:

enter image description here

可选:

如果需要确保分隔符从左到右覆盖所有宽度,您还可能需要以编程方式将单元格的边距设置为零。 在您的table view controller's Swift文件中,如有需要,请使用以下代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("YOURCELL", forIndexPath: indexPath) as! UITableViewCell

    // THIS IS THE IMPORTANT PART...
    cell.layoutMargins = UIEdgeInsetsZero
    cell.preservesSuperviewLayoutMargins = false

    return cell

}

如果表视图和单元格位于不同的 xib 文件中(没有故事板),也可以正常工作。 - Nicolas Miari
1
这也适用于没有故事板或xib的情况。如果您通过编程方式创建表格,则UITableView和UITableViewCell都公开了.separatorInset属性。将单元格的属性设置为.zero,让表格的属性成为您想要的值。 - NSRover

15

您可以通过Storyboard来设置分隔符。

对于标题,您可以像这样制作自定义标题。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
  UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
  UILabel *lblTitle = [UILabel.alloc initWithFrame:CGRectMake(6, 3, 136, 21)];

  [lblTitle setFont:[UIFont fontWithName:@"HelveticaNeue" size:13]];
  [lblTitle setTextColor:[UIColor blackColor]];
  [lblTitle setTextAlignment:NSTextAlignmentLeft];
  [lblTitle setBackgroundColor:[UIColor clearColor]];
  [viewHeader addSubview:lblTitle];

  return viewHeader;
}

给它任何特定的高度,并给它任何文本。为部分标题创建一个数组,其中包含您的年份。


viewHeader代表什么?它没有被初始化。 - Alessio Crestani
是的,我知道,但我不明白我必须放什么代替viewHeader,那是一个委托函数,我没有看到任何viewHeader的初始化:P - Alessio Crestani
viewHeader基本上是一个UIView,我们将返回它作为标题部分的使用。-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section是一个代理函数。将其放置在您的UITableView视图控制器中。 - Zeeshan
我现在看到的不是2013,而是空白O_O。 - Alessio Crestani
你是否创建了一个NSMutableArray并将你的年份分别添加到其中?如果没有,请创建。在你的-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:方法中,使用[lblTitle setText:[array objectAtIndex:section]来设置标题。 - Zeeshan

2

UITableView有一个名为viewForHeaderInSection的委托方法。如果您删除填充,然后在委托方法中添加填充并返回部分标题视图。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 200, 25)];

    // 15 pixel padding will come from CGRectMake(15, 5, 200, 25).
    label.text = @"2013";
    [view addSubview: label];
    return view;
}

您可以按照自己的喜好设计标题视图。要设置tableView标题的高度,请使用以下代码:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

希望这能帮助到您... )

我认为这是解决方法,但对我来说不太清楚。我需要重新实现- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 这个代理方法中如何将右边的填充移动15像素?你能给我一个例子吗? - Alessio Crestani
@AlessioCrestani>> 周末愉快。抱歉晚了 :) - Rashad

1
你可以在表格(包括页眉/页脚)和单元格本身上设置插入值:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
    }    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0, 10, 0, 0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

0
如果您想将节标题文本与左边距缩进,这里有一个我使用的简单方法。在函数中,看到第一个引号后面的空格了吗?只需在文本开始之前放置所需数量的空格即可。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let title: UILabel = UILabel()

        title.text = " \(sortedSections[section])"
        title.textAlignment = NSTextAlignment.Left

        return title
    }

0

有时候你可能不想总是创建自定义的表头视图,因为这有时会过度设计或需要一些试错才能使其与默认的分区标题完全相同。相反,有一个简单的技巧可以避免使用自定义表头视图。只需将分隔符缩进设置为0,并在标题字符串中添加一些前导空格即可。

enter image description here

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"    Your Title";
}

15
在字符串中添加空格从来不是一个有效的选项。 - DevZarak
在IOS 9中,至少在我的情况下,不需要任何空格,只需更改分隔符插图即可将标题文本移动到正确的位置...那太棒了。 - Erez

-4
在你的cellForRowAtIndeaPath方法中,像这样添加一个视图到单元格中:
UIView* view = [[UIView alloc] initWithFrame:CGrectmake(cell.frame.ori.x, cell.frame.ori.y, 20.0, cell.height)];

view.backgroundColor = desired color

[cell.containView addSubView: view];

将此代码添加到您的第一个单元格中即可完成


这不是一个单元格,TableView 有很多节。那只是一个例子。 - Alessio Crestani
这将适用于所有部分和单元格,您还可以根据特定的单元格或部分进行条件设置。 - Retro

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