当前位置: 首页 > 面试题库 >

如何快速将字典中的TableView项目分组?

端木震博
2023-03-14
问题内容

让我们考虑这个例子:

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet weak var tableView: UITableView!

    var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]

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

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")

    return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return ???
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int{      
    return names.count
    }

    func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{

    return ???
    }

    func tableView(tableView: UITableView,
        titleForHeaderInSection section: Int) -> String?{        
    return ????
    }
}

假设我们需要字典的键(水果和蔬菜)是部分的数量,加上它们将是这些部分的标题。键的项目(例如苹果和香蕉)将是每个部分的行。如何在我的代码中实现呢?我知道这可能很容易,但我无法弄清楚自己的自我。


问题答案:

您可以使用struct,这是示例:

import UIKit

class TableViewController: UITableViewController {

    var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]

    struct Objects {

        var sectionName : String!
        var sectionObjects : [String]!
    }

    var objectArray = [Objects]()

    override func viewDidLoad() {
        super.viewDidLoad()

        for (key, value) in names {
            println("\(key) -> \(value)")
            objectArray.append(Objects(sectionName: key, sectionObjects: value))
        }
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return objectArray.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objectArray[section].sectionObjects.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

        // Configure the cell...
        cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
        return cell
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        return objectArray[section].sectionName
    }
}


 类似资料:
  • 问题内容: 我找到了一个解决方案,但确实很慢: 您有没有使用外部模块(numpy等)的任何想法? 问题答案: 由于字典很大,因此最好将所有涉及的项都保留为迭代器和生成器,像这样 样品运行: 输出量

  • 我找到了一个解决方案,但速度非常慢: 不使用外部模块(Numpy等),你有什么想法吗?

  • 问题内容: Swift中的数组支持+ =运算符,可将一个数组的内容添加到另一个数组。有没有简单的方法可以做到这一点的字典? 例如: 问题答案: 您可以为定义运算符,例如

  • 问题内容: 快速将NSDictionaries写入文件有局限性。根据我从api文档中学到的知识和这个答案,键类型应该是NSString,值类型也应该是NSx类型,并且Int,String和其他swift类型可能不起作用。问题是,如果我有一个像这样的字典:如何快速将其写入plist文件或从plist文件读取? 问题答案: 无论如何,当您要存储到文件时,它必须是协议的子类并符合协议。像这样: 然后,这

  • 问题内容: 我正在尝试复制在目标C中执行的for循环,但遇到“’AnyObject’没有名为’GeneratorType’的成员错误: 这是我的雨燕 我试过为字典定义一个holder变量。任何人都可以看到我在做什么错吗? 问题答案: 这不是字典的循环。它循环通过存储在其中一个字典键中的数组。例如,如果您有一个字符串数组作为字典的键之一,这就是想要做的事情。 如果您确实想只遍历字典,这在Swift中

  • 我需要在javafx tableview中实现一个拥有庞大数据(大约10万)的过滤器, 我试过这个教程。它可以工作,但与swing排序和过滤相比,过滤速度非常慢。 谁能帮我提速吗。 现在正在发生的事情是,当我键入textproperty change fire up和filterdata时,但速度很慢,我需要一些东西来显示筛选结果,并在swing中快速键入。 提前谢谢。 p、 我也看过这个。