【问题标题】:TableView section seperator lineUITableView 部分分隔线
【发布时间】:2017-07-17 08:24:05
【问题描述】:

我想在表格视图部分添加分隔线。目前,标题部分视图的代码为:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    // recast your view as a UITableViewHeaderFooterView
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.backgroundView.backgroundColor = [UIColor clearColor];
    header.textLabel.textColor = [UIColor blackColor];
    [header.textLabel setFont:[UIFont fontWithName:@"Rubik-Regular" size:15.0]];

}

【问题讨论】:

  • 您的后退按钮在错误的一侧,请不要这样做
  • @Lope 你是指右上角的后退按钮吗?
  • 是的,那个。它违背了用户习惯的一切
  • @Lope 好的,感谢您的指正。

标签: ios objective-c xcode uitableview


【解决方案1】:

斯威夫特 4

 override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    let separatorView = UIView(frame: CGRect(x: tableView.separatorInset.left, y: footerView.frame.height, width: tableView.frame.width - tableView.separatorInset.right - tableView.separatorInset.left, height: 1))
    separatorView.backgroundColor = UIColor.separatorColor
    footerView.addSubview(separatorView)
    return footerView
}

extension UIColor {
   class var separatorColor: UIColor {
     return UIColor(red: 244.0/255.0, green: 244.0/255.0, blue: 244.0/255.0, alpha: 1.0)
   }
}

【讨论】:

  • 这是在 swif 中实现这一目标的最佳方式 :)
  • 如果您想使用与默认分隔符相同的颜色,我认为separatorView.backgroundColor = tableView.separatorColor 将是设置分隔符颜色的最佳方式,因为 Apple 可能随时更改默认颜色。
【解决方案2】:

如果你有

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

去那里会更好:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // recast your view as a UITableViewHeaderFooterView
    UITableViewHeaderFooterView *header = // make header here
    header.backgroundView.backgroundColor = [UIColor clearColor];
    header.textLabel.textColor = [UIColor blackColor];
    [header.textLabel setFont:[UIFont fontWithName:@"Rubik-Regular" size:15.0]];
    // make a view with height = 1 attached to header bottom
    UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, header.frame.size.height, header.frame.size.width, 1)];
    [separator setBackgroundColor:[UIColor yellowColor]];
    [header addSubview:separator];
    return header;
}

【讨论】:

  • “在此处制作标题”是什么意思?
  • 这不是 UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;对吗?
  • 对不起,我有一段时间没来了。通常在这种方法中,我自己制作页脚/页眉视图。 UIView *header = ... /create/load from nib/use cell/etc.您也可以在 willDisplayHeaderView 中添加分隔符,但在这种情况下,我认为您可能需要检查标题是否已经包含分隔符视图。不客气。
【解决方案3】:

我使用了下面的代码,它对我有用:

  • Swift 版本:4.2
  • Xcode 版本:10.3
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    let dummyView = UIView() //just a dummy view to return
    let separatorView = UIView(frame: CGRect(x: tableView.separatorInset.left, y: footerView.frame.height, width: tableView.frame.width - tableView.separatorInset.right - tableView.separatorInset.left, height: 0.5))
    separatorView.backgroundColor = UIColor.white
    footerView.addSubview(separatorView)

    if section == 1 {    
        return footerView
    }
    return dummyView
}

【讨论】:

    【解决方案4】:

    你可以这样做:

    CGRect sepFrame = CGRectMake(0, view.frame.size.height-1, 320, 1); 
    UIView *separatorView =[[UIView alloc] initWithFrame:sepFrame]; 
    seperatorView.backgroundColor = UIColor.yellow()
    [header addSubview:separatorView];
    

    【讨论】:

    • 你确定吗?因为如果您将这行代码添加到您的代码中,它应该可以工作。 @NurII 这与您标记为正确答案的答案相同。
    【解决方案5】:

    我发现添加页脚视图是最简洁的解决方案。不要忘记在 heightForFooterInSection 中包含页脚视图高度。

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let separatorView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: tableView.frame.width, height: 1.0))
        separatorView.backgroundColor = tableView.separatorColor
        return separatorView
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1.0
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-14
      • 2011-06-15
      • 2021-10-21
      • 2017-03-12
      • 2015-11-05
      • 2014-11-19
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      相关资源
      最近更新 更多