我想在表格视图部分添加分隔线。目前,标题部分视图的代码将是:
- (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]];
}
答案 0 :(得分:7)
Swift 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)
}
}
答案 1 :(得分: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;
}
答案 2 :(得分:1)
你可以这样做:
CGRect sepFrame = CGRectMake(0, view.frame.size.height-1, 320, 1);
UIView *separatorView =[[UIView alloc] initWithFrame:sepFrame];
seperatorView.backgroundColor = UIColor.yellow()
[header addSubview:separatorView];
答案 3 :(得分:1)
我使用下面的代码,它对我有用:
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 :(得分:0)
我发现添加页脚视图是最干净的解决方案。不要忘记在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
}