简体   繁体   English

UITableView,分隔符颜色在哪里设置?

[英]UITableView, Separator color where to set?

I have added a UITableView in IB and set the "delegate" and "datasource" and all is working well. 我在IB中添加了一个UITableView并设置了“delegate”和“datasource”,一切运行良好。 What I wanted to do next was change the separator color, but the only way I could find to do this was to add the method to one of the delegate callbacks, is there a better place I should put this? 我接下来要做的是更改分隔符颜色,但我能找到的唯一方法是将方法添加到其中一个委托回调中,是否有更好的地方我应该这样做?

I don't have this at the moment but I was thinking that maybe I need to add an "iVar" from my controller that I can link to the UITableView in IB and then set separator color in the viewDidload ? 我目前没有这个,但我想也许我需要从我的控制器添加一个“iVar”,我可以链接到IB中的UITableView ,然后在viewDidload设置分隔符颜色?

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView setSeparatorColor:[UIColor blackColor]];
    return 65;
}
- (void)viewDidLoad
{
   [self.tableView setSeparatorColor:[UIColor myColor]];
}

I hope that helps - you'll need the self. 我希望有所帮助 - 你需要self. to access it, remember. 访问它,记住。

Swift 4.2 Swift 4.2

tableView.separatorColor = UIColor.red

Now you should be able to do it directly in the IB. 现在你应该能够直接在IB中完成它。

Not sure though, if this was available when the question was posted originally. 但是不确定,如果在最初发布问题时这是可用的。

在此输入图像描述

Swift version: Swift版本:

override func viewDidLoad() {
    super.viewDidLoad()

    // Assign your color to this property, for example here we assign the red color.
    tableView.separatorColor = UIColor.redColor()
}

Try + (instancetype)appearance of UITableView: 尝试UITableView的+(instancetype)外观

Objective-C: Objective-C的:

[[UITableView appearance] setSeparatorColor:[UIColor blackColor]]; // set your desired colour in place of "[UIColor blackColor]"

Swift 3.0: Swift 3.0:

UITableView.appearance().separatorColor = UIColor.black // set your desired colour in place of "UIColor.black"

Note: Change will reflect to all tables used in application. 注意:更改将反映到应用程序中使用的所有表。

If you just want to set the same color to every separator and it is opaque you can use: 如果您只想为每个分隔符设置相同的颜色并且它是不透明的,您可以使用:

 self.tableView.separatorColor = UIColor.redColor()

If you want to use different colors for the separators or clear the separator color or use a color with alpha. 如果要为分隔符使用不同的颜色或清除分隔符颜色或使用带alpha的颜色。

BE CAREFUL: You have to know that there is a backgroundView in the separator that has a default color. 小心:您必须知道分隔符中有一个具有默认颜色的backgroundView。

To change it you can use this functions: 要更改它,您可以使用此功能:

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if(view.isKindOfClass(UITableViewHeaderFooterView)){
            var headerView = view as! UITableViewHeaderFooterView;
            headerView.backgroundView?.backgroundColor = myColor

           //Other colors you can change here
           // headerView.backgroundColor = myColor
           // headerView.contentView.backgroundColor = myColor
        }
    }

    func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        if(view.isKindOfClass(UITableViewHeaderFooterView)){
            var footerView = view as! UITableViewHeaderFooterView;
            footerView.backgroundView?.backgroundColor = myColor
           //Other colors you can change here
           //footerView.backgroundColor = myColor
           //footerView.contentView.backgroundColor = myColor
        }
    }

Hope it helps! 希望能帮助到你!

Swift 3, xcode version 8.3.2, storyboard->choose your table View->inspector->Separator. Swift 3,xcode版本8.3.2,storyboard->选择你的表View-> inspector-> Separator。

Swift 3,xcode版本8.3.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM