xcode TableView 部分分隔线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45139262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:08:21  来源:igfitidea点击:

TableView section seperator line

iosobjective-cxcodeuitableview

提问by Nur II

I wanted to add separator line into the table view section. Currently the code for the header section view will be:

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

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

}

enter image description here

在此处输入图片说明

回答by rshankar

Swift 4

斯威夫特 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)
   }
}

回答by Eridana

If you have

如果你有

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

would be better to make it there:

最好让它在那里:

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

回答by Pedro Pinho

You can do like this:

你可以这样做:

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

回答by Imtiyaz Ahmad

I used below code and it worked for me:

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

  • Swift version : 4.2
  • Xcode version : 10.3
  • 斯威夫特版本: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
}