ios - 如何明智地显示 UITableView 单元格类别?

标签 ios uitableview

我想要一个 UITableView 来显示以下数据:

required output

代替:

my current output

我使用的代码如下:

// Data model class defining an Animal
class Animal {

    // data members
    var name: String
    var type: String

    // initializer
    init(name: String, type: String) {
        self.name = name
        self.type = type
    }

}

// View Controller
import UIKit

class AnimalsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // an array of Animal(s)
    var animals: [Animal] = [Animal("Cat", "Domestic"), Animal("Dog", "Domestic"), Animal("Lion", "Wild"), Animal("Deer", "Wild"), Animal("Tiger", "Wild")];

    override func viewDidLoad() {
        super.viewDidLoad();

    }

    // returns the number of rows to be present in table
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return animals.count
    }

    // returns each cell to be present in the table
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentfier = "animalbasic"
        // uses a protype cell from storyboard
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentfier)
        cell.textLabel.text = animals[indexPath.row].name
        return cell
    }
}

为了获得所需的输出,我需要在我的代码中更改什么?(假设数组始终按照 Animal.type 排序)

最佳答案

您需要按类型将数据分成几个部分。

class AnimalsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var animals: [Animal] = [Animal("Cat", "Domestic"), Animal("Dog", "Domestic"), Animal("Lion", "Wild"), Animal("Deer", "Wild"), Animal("Tiger", "Wild")]

    var sections: [[Animal]] = []

    override func viewDidLoad() {
        super.viewDidLoad();

        var sectionForType = [String: [Animal]]()
        for animal in animals {
            if sectionForType[animal.type] == nil { sectionForType[animal.type] = [] }
            sectionForType[animal.type]!.append(animal)
        }
        sections = sectionForType.keys.sorted().map({ sectionForType[$0]! })
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section][0].type
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "animalbasic")!
        cell.textLabel!.text = sections[indexPath.section][indexPath.row].name
        return cell
    }
}

关于ios - 如何明智地显示 UITableView 单元格类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43473828/

相关文章:

ios - NSDateFormatter 无法为设置了 12 小时制的英国地区返回日期时间

objective-c - iOS – setEditing [super setEditing...] 和/或 [tableView setEditing...]

ios - 重复的对象被追加到数组 swift firebase 中

ios - 让我们缩短 cell.textLabel 的长度

当应用程序未连接到 xcode 时,iOS 8 静默推送通知不会触发 didReceiveRemoteNotification 方法

ios - 将我的应用程序转换为新的 Swift - 错误 "Extra argument in call"

iOS:使用 anchor 以编程方式将 UIImageView 与底部对齐

ios - heightForImageCellAtIndexPath 中 dispatch_once 的原因

ios - 带有 UITableView Swift 3 的可折叠输入表单

uitableview - MvvmCross TableView 绑定(bind)中的 "System.InvalidOperationException: Collection was modified"