以编程方式将 UITableViewStyle 设置为 UITableView 插座

Set UITableViewStyle to UITableView outlet programmatically

假设,我有以下 ViewController,有一个插座 tableView

class CustomController: UIViewController, UITableViewDataSource, ... {
    @IBOutlet var tableView: UITableView?
}

CustomController 在故事板的右侧控制器中设置为 Class 属性。 tableViewStyle 在故事板中也设置为 Grouped
现在我遇到了一个问题——我需要我的 UITableViewStyletableView, 有风格 Plain。有什么想法可以实现吗?

很难理解你的问题。您可以在情节提要中更改它。

您不能手动更改它

public var style: UITableViewStyle { get }

也许您需要重新创建 tableView

public init(frame: CGRect, style: UITableViewStyle)

我认为你不应该使用 UITableView style 并进行自定义或样式化 UITableViewCell

作为@katleta3000 mentioned 可能的解决方案是UITableViewHeaderFooterView。我稍微试验了一下,发现我的 tableView 风格 Grouped 可以看起来像 Plain。我只是简单地像这样创建我的 tableHeaderView

self.tableView!.tableHeaderView = UIView(frame: CGRectMake(0, 0, self.view.bounds.size.width, CGFloat.min))

现在我的 tableView 看起来像我想要的(即 "without" header)。

P.S。我不接受我的回答是正确的。希望有人找到更好的解决方案。


更新。
更好的方法是为 header 设置高度:

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