ios - 如何在 UITableView 中基于行输入(字母)添加另一个部分 - swift
问题描述
我是一个新的iOS编程。我正在创建一个类似于contact
app 的示例应用程序。我想知道那个真正的应用程序是如何工作的。例如,我们的 tableview 显示名称基于字母,例如 name 开头A
will stays in A
section 和B
will stay in B
section 等等。当用户输入部分中不存在的名称时,将自动创建新部分。我想知道我怎样才能实现这样的目标。
这就是我以编程方式创建 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
部分中等等...
谢谢你的解释。
解决方案
最简单的解决方案是方法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
如果您想要在插入部分和行时使用动画的更复杂的解决方案,您必须自己编写逻辑。可以使用键的设计数组和名称的字典以及自定义结构,包括字母索引和行数组。
推荐阅读
- git - VSCode 无法使用 Github
- android - android studio 中的 RemoveBG API
- java - 如何在 Intellij Idea 中更新 PostgreSQL 数据库
- react-native - 我在 React-Native 中遇到了这个意外错误
- amazon-web-services - Elastic Beanstalk 通过 CloudFormation 将标签添加到自动创建的 S3 存储桶
- ios - 正在录制的视频文件上传到服务器
- android - 错误“没有打开应用程序”RobotFramework Mobile 测试脚本在 saucelabs 上执行
- c++ - i+1 之间的差异
我的 C++ 代码
#include <bits/stdc++.h> using namespace std; #define ll long long #define test() ll t; cin>>t; while(t--) int main () { test()
- java - 如何将毫秒转换为模式 mm:ss for kotlin
- python - Python 多处理 Pool.starmap 更改 np.array 列的数据类型