ios 如何在 Swift 中以编程方式创建带有部分的分组 TableView

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

How to create a Grouped TableView with sections programmatically in Swift

iosswiftuitableview

提问by Nick89

I would like to know how to create a grouped/sectioned tableview programmatically. The design I am trying to create is this:

我想知道如何以编程方式创建分组/分段的 tableview。我正在尝试创建的设计是这样的:

enter image description here

在此处输入图片说明

I want there to be a UIImageViewat the top with a 160 height, followed by 3 sections, each with a different number of rows. I will then place static labels in each row and then update their labels with my data.

我希望UIImageView顶部有一个 160 高的区域,然后是 3 个部分,每个部分都有不同的行数。然后我将在每一行中放置静态标签,然后用我的数据更新它们的标签。

My code for this VC is as follows:

我的这个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

}

}

I have tried adding the method to state the numberOfSectionsetc but I don't seem to see them when I build the app. All I see is a normal looking tableview. Am I missing some setup?

我尝试添加方法来说明numberOfSections等,但在构建应用程序时似乎没有看到它们。我所看到的只是一个普通的 tableview。我错过了一些设置吗?

Because I am navigating to this VC programmatically, I need to set this up without using the storyboard.

因为我以编程方式导航到这个 VC,所以我需要在不使用故事板的情况下进行设置。

Any help on how to create the look I need above is appreciated, thanks!

感谢任何有关如何创建我需要的外观的帮助,谢谢!

回答by Brian Gorman

When combining other views with a TableView I prefer to make a UIViewController and use a UITableViewController as an embedded controller.

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

I committed an example on githubcheck it out

在github上提交了一个例子看看

Here is what your table controller code should look like:

这是您的表控制器代码应如下所示:

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

    }
}

回答by chiarotto.alessandro

First you should return 3 section using the following method

首先,您应该使用以下方法返回 3 个部分

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

and then according to the section number passed to this method return 1,2 and 4 (rows) for each section:

然后根据传递给此方法的节号为每个节返回 1,2 和 4(行):

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // return if section == 0 return 1 and so on...
}

finally use indexPath passed to this method to return the appropriate content according to section and row stored in indexPath:

最后使用传递给该方法的 indexPath 来根据 indexPath 中存储的 section 和 row 返回相应的内容:

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

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

  // if indexPath.section == 0 {
  //  cell.textLabel?.text = film?.Directed_By
  //  cell.detailTextLabel?.text = film?.Film_Poster_Image_URL
  // } else if ....


    return cell

}

回答by Avt

You forgot to call reloadData. Update viewDidLoadmethod to

你忘记打电话了reloadData。更新viewDidLoad方法为

override func viewDidLoad() {
    super.viewDidLoad()

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

If you need 3 sections with different number of rows change your methods to

如果您需要具有不同行数的 3 个部分,请将您的方法更改为

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

    return 3
}

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

To add an image to the top use UITableView's property tableHeaderView.

要将图像添加到顶部,请使用 UITableView 的属性tableHeaderView