【问题标题】:swift how to add uitableview group in uiviewcontroller in swiftswift 如何在 swift 中的 uiviewcontroller 中添加 uitableview 组
【发布时间】:2016-05-11 02:02:51
【问题描述】:

我想做一个类似设置的界面 Setting 我想我需要使用uitableview。我想在 uiviewcontroller 中使用 uitableview。但我不知道该怎么做...

我试图在 uiviewcontroller 中添加一个分组的 uitableview,但是当我运行应用程序时,分组的 uitableview 没有显示在 uiviewcontroller 中,我想我还远远没有做到这一点..

我应该使用哪种类型的 uitablview?分组?静态单元格?

你能告诉我怎么做吗?谢谢你。你有一些教程或开源学习吗?谢谢。

【问题讨论】:

    标签: swift uitableview uiviewcontroller


    【解决方案1】:

    您应该使用添加了部分的 UITableView。有一个很好的教程here

    更新

    首先,通过拖动到视图控制器上创建一个 UITableView。添加必要的约束,并生成原型单元。

    将单元重用标识符设置为单元。

    然后,Control 左键单击并将 tableView 拖动到 ViewController(顶部的黄色圆圈)。这样做两次并将其分配为 DataSource 和 Delegate。

    打开助手编辑器并控制将tableView拖到类中以创建IBOutlet。然后在你的类声明中添加UITableViewDelegate

    class ViewController: UIViewController, UITableViewDelegate {
    

    完成此操作后,创建两个新的空白 Swift 文件。文件,新建,文件。

    标题一个文件 Section.swift 和另一个 SectionsData.swift。

    在文件 Section.swift 中,添加此代码。

    struct Section 
    {
    var heading : String
    var items : [String]
    
    init(title: String, objects : [String]) {
    
        heading = title
        items = objects
    }
    }
    

    您在这里定义了一个结构,以便以后可以获取数据。

    在 SectionsData 文件中放入以下代码。您可以在此处编辑进入表格的内容。

    class SectionsData {
    
    func getSectionsFromData() -> [Section] {
    
    
        var sectionsArray = [Section]()
    
        let hello = Section(title: "Hello", objects: ["Create", "This", "To", "The"])
        let world = Section(title: "World", objects: ["Extent", "Needed", "To", "Supply", "Your", "Data"])
        let swift = Section(title: "Swift", objects: ["Swift", "Swift", "Swift", "Swift"])
    
    
        sectionsArray.append(hello)
        sectionsArray.append(world)
        sectionsArray.append(swift)
    
        return sectionsArray
    }
    }
    

    在此文件中,您创建了一个类,然后创建了一个函数来保存和检索数据。

    现在,在包含您的 tableview 的 IBOutlet 的文件中,创建以下变量。

    var sections: [Section] = SectionsData().getSectionsFromData()
    

    现在已经完成了艰苦的工作,是时候填充表格了。以下函数允许这样做。

    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return sections.count
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return sections[section].items.count
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    {
        return sections[section].heading
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    
        cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
    
        return cell
    }
    

    你应该能够运行它并得到你想要的结果。您可以在提供数据时编辑单元格外观。例如,

    cell.textLabel?.font = UIFont(name: "Times New Roman", size: 30)
    

    只要确保像这样更改字体时,字符串名称的拼写是准确的。

    我希望这会有所帮助。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @sunqingyao 明白了,以后我会做得更好。谢谢!
    【解决方案2】:

    您可以为此使用普通的 uitableview,对于“组”,您应该将您的 tableview 分成几个部分(在代码中,您有一个返回部分数量的委托函数)

    要在 UIViewController 中使用 tableview,您应该从 UITableViewDelegate 和 UITableViewDataSource 类中实现。然后在你的 viewdidload 中你可以放置你的 tableview.datasource = self 和 tableview.delegate = self 的名称,然后你可以在你的 UIViewController 中使用 tableview 的委托函数。

    在快速谷歌之后,我找到了一个可能对你有用的例子: LINK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2015-04-29
      • 2016-11-05
      • 2019-08-01
      • 2017-01-04
      相关资源
      最近更新 更多