如何将 UITableView 设置为分组样式

我有一个 UITableViewController子类与部分。部分显示为默认样式(没有圆角)。如何将 TableView 样式设置为在代码中分组?我不用 Interface Builder,所以我需要

[self.tableView setGroupedStyle]

我在 Stack Overflow 上搜索了一下,但是没有找到答案。

120923 次浏览
self.tableView.style = UITableViewStyleGrouped

EDIT:

Had assumed this was a read/write property. In that case, you can either follow Dimitris advice and set the style when you instantiate the controller, or (if you're using a XIB), you can set it via IB.

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

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

You can do the following:

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

Swift 3:

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

You can also try to make the separator line color clear which could give the grouped style effect:

[myTVContoller.tableView setSeparatorColor:[UIColor clearColor]];

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

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

You can use:

(instancetype)init {
return [[YourSubclassOfTableView alloc] initWithStyle:UITableViewStyleGrouped];
}

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

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


if (self)
{


}
return self;
}

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

Swift 4+:

let myTableViewController = UITableViewController(style: .grouped)

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)

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:

let settingsController = SettingsViewController(style: .Grouped)

If you have one TableView for more tables, and one of this tables is grouped and the another one plain, than you can simulate the plain style with the function from UITableViewDelegate:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.min
}

You can do this with using storyboard/XIB also

  1. Go To storyboard -> Select your viewController -> Select your table
  2. Select the "Style" property in interface-builder
  3. Select the "Grouped"
  4. Done

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

enter image description here

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

Objective C:

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

Swift:

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

Swift 4

Using Normal TableView

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

Using TPKeyboardAvoidingTableView

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

swift 4

if you don't want use storyboard, this might be help.

you can add table view and set properties in a closure:

lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .grouped)


tableView.backgroundColor = UIColor(named: Palette.secondaryLight.rawValue)
tableView.rowHeight = 68
tableView.separatorStyle = .none
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()

then add in subview and set constraints.

If you create your UITableView in code, you can do the following:

class SettingsVC: UITableViewController {


init() {
if #available(iOS 13.0, *) {
super.init(style: .insetGrouped)
} else {
super.init(nibName: nil, bundle: nil)
}
}
    

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

If you're not using Storyboards and just want a clean, concise way to setup your UITableViewController subclass to use a different tableView style, you can simply override loadView like this:

override func loadView() {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.delegate = self
tableView.dataSource = self
view = tableView
}

As the documentation says:

loadView creates the view that the controller manages it's a method you can ovveride when using ViewControllers whose views are not defined via Storyboards or NIBs.

The UITableViewController implementation already overrides this method for you to crate a simple, plain UITableView as the root view for the controller, but nothing stops you from further overriding it to create a table view that better suits your needs.

Just remember:

Your custom implementation of this method should not call super.