如何在Swift中以编程方式创建带有节的Grouped TableView

Nic*_*k89 5 uitableview ios swift

我想知道如何以编程方式创建分组/分段表格视图。我试图创建的设计是这样的:

在此处输入图片说明

我希望UIImageView在顶部具有160的高度,然后是3个部分,每个部分具有不同的行数。然后,我将在每行中放置静态标签,然后使用我的数据更新其标签。

我为此VC编写的代码如下:

import UIKit

class SelectedFilmTableViewController: UITableViewController {

var film: Film? {
    didSet {
        navigationItem.title = film?.Film_Name
    }
}

var cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = film?.Directed_By
    cell.detailTextLabel?.text = film?.Film_Poster_Image_URL

    return cell

}

}
Run Code Online (Sandbox Code Playgroud)

我尝试添加方法来声明numberOfSections等,但是在构建应用程序时似乎看不到它们。我看到的只是一张普通的表格视图。我是否缺少某些设置?

因为我以编程方式导航到此VC,所以需要在不使用情节提要的情况下进行设置。

感谢您提供有关如何在上面创建外观的帮助,谢谢!

小智 5

当将其他视图与TableView结合使用时,我更喜欢制作UIViewController并将UITableViewController用作嵌入式控制器。

我公司承诺在github一个例子看看

这是表控制器代码的外观:

class MyTableController : UITableViewController {
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if(section == 2) {
            return "Film Information"
        } else {
            return " "
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(section == 0) {
            return 1
        } else if(section == 1) {
            return 2
        } else {
            return 4
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("SomeCell", forIndexPath: indexPath) as UITableViewCell


        return cell

    }
}
Run Code Online (Sandbox Code Playgroud)