ios 如何将 UITableView 设置为分组样式

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

How can I set a UITableView to grouped style

iosobjective-cuitableviewcocoa-touch

提问by nevan king

I have a UITableViewControllersubclass with sections. The sections are showing with the default style (no rounded corners). How can I set the TableView style to grouped in the code? I'm not using Interface Builder for this, so I need something like

我有一个UITableViewController带有部分的子类。这些部分以默认样式(无圆角)显示。如何在代码中将 TableView 样式设置为分组?我没有为此使用 Interface Builder,所以我需要类似的东西

[self.tableView setGroupedStyle]

I searched on Stack Overflow, but couldn't come up with an answer.

我在 Stack Overflow 上搜索过,但找不到答案。

回答by Lior

You can do the following:

您可以执行以下操作:

UITableView *myTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

Swift 3:

斯威夫特 3:

let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)

回答by Dimitris

If i understand what you mean, you have to initialize your controller with that style. Something like:

如果我明白你的意思,你必须用那种风格初始化你的控制器。就像是:

myTVContoller = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];

回答by Thomas Decaux

I give you my solution, I am working in "XIB mode", here the code of a subclass of a UITableViewController :

我给你我的解决方案,我在“XIB 模式”下工作,这里是 UITableViewController 子类的代码:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    return self;
}

回答by Gagan Joshi

Below code Worked for me, I am also using UITableview class

下面的代码对我有用,我也在使用 UITableview 类

- (id)initWithStyle:(UITableViewStyle)style
{
     self = [super initWithStyle:UITableViewStyleGrouped];

     if (self)
    {

     }
    return self;
}

回答by DZoki019

If you are inheriting UITableViewController, you can just init tableView again.

如果你继承了 UITableViewController,你可以再次初始化 tableView。

Objective C:

目标 C:

self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

Swift:

迅速:

self.tableView = UITableView(frame: CGRect.zero, style: .grouped)

回答by Fangming

Setting that is not that hard as mentioned in the question. Actually it's pretty simple. Try this on storyboard.

设置并不像问题中提到的那么难。其实很简单。在故事板上试试这个。

enter image description here

在此处输入图片说明

回答by Md. Ibrahim Hassan

Swift 4

斯威夫特 4

Using Normal TableView

使用普通 TableView

let tableView = TableView(frame: .zero, style: .grouped)

Using TPKeyboardAvoidingTableView

使用 TPKeyboardAvoidingTableView

let tableView = TPKeyboardAvoidingTableView(frame: .zero, style: .grouped)

回答by Zeesha

For set grouped style in ui itself:-Select the TableView then change the "style"(in attribute inspector)) from plain to Grouped.

对于在 ui 本身中设置分组样式:-选择 TableView 然后将“样式”(在属性检查器中))从普通更改为分组。

回答by Juan Boero

Swift 4+:

斯威夫特 4+:

let myTableViewController = UITableViewController(style: .grouped)

回答by Jordi Bruin

You can also do this if you want to use it on a subclass you've already created in a separate swift file (probably not 100% correct but works)

如果您想在单独的 swift 文件中已经创建的子类上使用它,您也可以这样做(可能不是 100% 正确但有效)

override init(style: UITableViewStyle) {
    super.init(style: style)
    UITableViewStyle.Grouped
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Now in you appdelegate.swift you can call:

现在在你 appdelegate.swift 你可以调用:

let settingsController = SettingsViewController(style: .Grouped)