【问题标题】:Keep UITableView padding on header but no in separator on IOS7在 IOS7 上保持 UITableView 填充在标题上但没有在分隔符中
【发布时间】:2014-01-27 19:33:25
【问题描述】:

在 iOS7 上,分隔符在左侧有 15px 的填充。我知道我可以在 xib 文件中的 UITableView 设置上使用分隔符插入功能删除此填充,但我需要保留带有填充的标题文本。怎么做?

默认值:

自定义分隔符插入为 0:

我需要保留如图 2 所示的分隔符,但带有“2013”​​的标题如图 1 所示。

【问题讨论】:

  • tableview 中只有一个部分还是更多?
  • 不,在我收到通知的所有日子里都有一个。

标签: ios iphone uitableview ios7 separator


【解决方案1】:

使用 SWIFT 和故事板

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

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

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

可选:

您可能还需要以编程方式将单元格的边距设置为零,以确保分隔符从左到右一直延伸。如果需要,请在表视图控制器的 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

}

【讨论】:

  • 如果表格视图和单元格位于单独的 xibs(无情节提要)中也可以使用。
  • 这也适用于没有故事板或 xibs 的情况。如果您以编程方式创建表,则 UITableView 和 UITableViewCell 都会公开 .separatorInset 属性。将单元格的一设置为.zero,并让表格的一成为您想要的值。
【解决方案2】:

对于分隔符,您可以通过情节提要设置它

为 header 创建一个像这样的自定义 header

- (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 代表什么?它没有初始化
  • 是的,我知道,但我不明白我必须放什么而不是 viewHeader,这是一个委托函数,我在里面看不到任何 viewHeader 初始化:P
  • viewHeader 基本上是一个 UIView,我们将返回它用作标题部分。 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 是一个委托函数。把它放在你的 UITableView 视图控制器中。
  • 我没有看到任何东西,而不是 2013 现在 O_O
  • 您是否制作了一个 NSMutableArray 并分别在其中添加了您的年份?如果不这样做。在你-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:[lblTitle setText:[array objectAtIndex:section]
【解决方案3】:

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;以及如何在这个委托内移动 15px 右填充?能给我举个例子吗?
  • @AlessioCrestani>> 周末。对不起,来晚了。 :)
【解决方案4】:

您可以在表格(和页眉/页脚)和单元格本身上设置插图:

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

【讨论】:

    【解决方案5】:

    您可能不想总是创建自定义标题视图,因为有时它有点过分,或者需要一些试验和错误才能使其与默认部分标题完全相同。相反,有一个简单的 hack 不需要返回自定义标题视图。只需将分隔符缩进设置为 0 并在标题字符串中放置一些前导空格。

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

    【讨论】:

    • 向字符串添加空格绝不是一个有效的选项。
    • 在 IOS 9 中,在我的情况下,在列表中,不需要任何空格,只需更改分隔符插入即可将标题文本移动到正确的位置...。太好了
    【解决方案6】:

    如果你想从左边距缩进部分文本,这是我做的一个简单的方法。在函数中,看到第一个引号后的空格了吗?只需在文本开始前放置所需数量的空格即可。

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let title: UILabel = UILabel()
    
            title.text = " \(sortedSections[section])"
            title.textAlignment = NSTextAlignment.Left
    
            return title
        }
    

    【讨论】:

      【解决方案7】:

      在你的 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 有很多部分。这只是一个例子。
      • 这将适用于所有部分和单元格,您也可以使用特定的单元格或部分对其进行调节
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2013-08-05
      • 2012-10-16
      • 2023-03-16
      相关资源
      最近更新 更多