gpt4 book ai didi

ios - 如何根据 UITableView 中的行输入(字母)添加另一个部分 - swift

转载 作者:行者123 更新时间:2023-11-28 05:54:51 25 4
gpt4 key购买 nike

我是iOS编程新手。我正在创建一个类似于 contact 应用程序的示例应用程序。我想知道那个真正的应用程序是如何工作的。例如,我们的 TableView 显示名称基于字母表,如以 A 开头的名称将保留在 A 部分,而 B 将保留在 B 部分等等。并且当用户输入一些节中不存在的名称时,将自动创建新节。我想知道我怎样才能实现这样的目标。

这就是我以编程方式创建 UITableView 的方式。不使用 Storyboard

private let cellId = "cellId"
private let array = ["Aname", "Bname", "Cname", "Dname"]
override func viewDidLoad() {
super.viewDidLoad()

tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
}

这里是我如何自定义显示数据

// Table View

扩展 ContactListController {

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let label = UILabel()
label.backgroundColor = .gray
label.text = "A"
return label

}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


return array.count


}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)


cell.textLabel?.text = array[indexPath.row]



return cell

}

对于我想要的输出,我想显示以 A 开头的名称应该在 A 部分等等......

感谢您的解释。

最佳答案

最简单的解决方案是方法 Dictionary(grouping: by:)

  • 为部分 (keys) 创建一个数组,为名称 (people) 创建一个字典,并使 array 可变

    var keys = [String]()
    var people = [String : [String]]()

    private var array = ["Aname", "Bname", "Cname", "Dname"]
  • 添加数据分组方法

    func groupData()
    {
    people = Dictionary(grouping: array, by: {String($0.first!)})
    keys = people.keys.sorted()
    tableView.reloadData()
    }

    如果要限制分组只考虑大写字母写

    people = Dictionary(grouping: array, by: {$0.range(of: "^[A-Z]", options: .regularExpression) != nil ? String($0.first!) : "Unknown"})
  • viewDidLoad中调用方法

    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    groupData()
    }
  • 代替 viewForHeaderInSection 实现 titleForHeaderInSection

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return keys[section]
    }
  • 其他相关的 TableView 数据源和委托(delegate)方法是

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let letter = keys[section]
    return people[letter]!.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
    let letter = keys[indexPath.section]
    let item = people[letter]![indexPath.row]
    cell.textLabel!.text = item
    return cell
    }

要添加新项目,请将名称附加到 array 并调用 groupData

如果您想要在插入部分和行时使用动画的更复杂的解决方案,您必须自己编写逻辑。可以使用键的设计数组和名称的字典以及自定义结构,包括字母索引和行数组。

关于ios - 如何根据 UITableView 中的行输入(字母)添加另一个部分 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51721690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com