首页 > 解决方案 > 如何在 UITableView 中基于行输入(字母)添加另一个部分 - swift

问题描述

我是一个新的iOS编程。我正在创建一个类似于contactapp 的示例应用程序。我想知道那个真正的应用程序是如何工作的。例如,我们的 tableview 显示名称基于字母,例如 name 开头Awill stays in Asection 和Bwill stay in Bsection 等等。当用户输入部分中不存在的名称时,将自动创建新部分。我想知道我怎样才能实现这样的目标。

这就是我以编程方式创建 UITableView 的方式。不使用情节提要

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

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

这是我自定义显示数据的方式

// Table View

扩展联系人列表控制器 {

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部分中等等...

谢谢你的解释。

标签: iosswiftuitableview

解决方案


最简单的解决方案是方法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]
    }
    
  • 其他相关的表视图数据源和委托方法是

    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

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


推荐阅读