2017-07-17 83 views
0

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

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

+0

后退按钮是错误的一方,请不要做 – Lope

+0

@Lope你的意思右上方后退按钮? –

+0

是的,那个。它违背了用户习惯的一切 – Lope

回答

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; 
} 
+0

“make header here”的意思是? –

+0

是不是这个UITableViewHeaderFooterView * header =(UITableViewHeaderFooterView *)view;是正确的? –

+0

但是这个答案对我有用,谢谢Eridana –

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]; 
+0

我试过了,它不适用于我 –

+0

确定吗?因为如果你将这些代码行添加到代码中,它应该可以工作。 @NurII这与您标记为正确答案的答案相同。 – ppinho

0

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