Ios 使用Swift对UITableview中的数据进行分组和排序
我正在寻找一个类似于iPhone联系人的用户列表,并对其进行分组和排序。我想添加Ios 使用Swift对UITableview中的数据进行分组和排序,ios,swift,uitableview,sorting,sections,Ios,Swift,Uitableview,Sorting,Sections,我正在寻找一个类似于iPhone联系人的用户列表,并对其进行分组和排序。我想添加sectionIndexTitlesForTableView(:)、headerinsection(:)标题和sectionindextitle(:)。我还没有找到一个关于如何做到这一点的教程,我已经被困了好几个星期了 到目前为止,我能够检索用户并填充表视图。我还实现了UISearchBarDelegate var users: [BackendlessUser] = [] var filteredUsers :
sectionIndexTitlesForTableView(:)
、headerinsection(:)标题和sectionindextitle(:)
。我还没有找到一个关于如何做到这一点的教程,我已经被困了好几个星期了
到目前为止,我能够检索用户并填充表视图。我还实现了UISearchBarDelegate
var users: [BackendlessUser] = []
var filteredUsers : [BackendlessUser] = []
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
return users.count
} else {
return self.filteredUsers.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if tableView == self.tableView {
let user = users[indexPath.row]
cell.textLabel?.text = user.name
} else {
let filteredUser = filteredUsers[indexPath.row]
cell.textLabel?.text = filteredUser.name
}
return cell
}
您必须有一个数组字典(例如名称“data”) 开始:
let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var headers: [String] = []
var data : [String: [String]] = [:] // Choose your type
override func viewDidLoad(){
// Do your stuff...
headers = letters.keys.sort()
// init your data var
data = ...
tableView.reloadData()
}
- 对于标题:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return headers.count } func sectionHeaderTitlesForTableView(tableView: UITableView) -> [String]?{ return headers } func tableView: UITableView, titleForHeaderInSection section: Int) -> String?{ return headers[section]; }
- 细胞
func tableView(tableView: UITableView, numberOfRowInSection section: Int) -> Int { // Exemple return data[section].count }
func tableView(tableView: UITableView, numberOfRowInSection section: Int) -> Int {
// Exemple
return data[section].count
}