ios UITableView,Separator 颜色在哪里设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3364859/
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-08-30 17:29:17  来源:igfitidea点击:

UITableView, Separator color where to set?

iosobjective-cuitableviewcocoa-touch

提问by fuzzygoat

I have added a UITableViewin IB and set the "delegate" and "datasource" and all is working well. 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?

UITableView在 IB 中添加了一个并设置了“委托”和“数据源”,一切正常。我接下来想做的是更改分隔符颜色,但我能找到的唯一方法是将方法添加到委托回调之一,我应该把它放在更好的地方吗?

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 UITableViewin IB and then set separator color in the viewDidload?

我目前没有这个,但我想也许我需要从我的控制器中添加一个“iVar”,我可以链接到UITableViewIB,然后在viewDidload?

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

回答by Helen

- (void)viewDidLoad
{
   [self.tableView setSeparatorColor:[UIColor myColor]];
}

I hope that helps - you'll need the self.to access it, remember.

我希望这会self.有所帮助 - 你需要访问它,记住。

Swift 4.2

斯威夫特 4.2

tableView.separatorColor = UIColor.red

回答by Myxtic

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.

但不确定,如果最初发布问题时可用。

enter image description here

在此处输入图片说明

回答by King-Wizard

Swiftversion:

迅捷版:

override func viewDidLoad() {
    super.viewDidLoad()

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

回答by Nitesh Borad

Try + (instancetype)appearanceof UITableView:

试试+ (instancetype)UITableView 的外观

Objective-C:

目标-C:

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

Swift 3.0:

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

注意:更改将反映到应用程序中使用的所有表。

回答by Giang

Swift 3, xcode version 8.3.2, storyboard->choose your table View->inspector->Separator.

Swift 3,xcode 版本 8.3.2,storyboard->选择你的表格视图->检查器->分隔符。

Swift 3, xcode version 8.3.2

Swift 3,xcode 版本 8.3.2

回答by ezefire

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.

小心:您必须知道分隔符中有一个具有默认颜色的背景视图。

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!

希望能帮助到你!