我有一个TableView设置,并且试图在“正确标题”下过滤正确的联系人姓名;标记为(A-Z)。我从API中提取联系人姓名;我在结构中构造数据。目前,我的表格正在复制每个(A-Z)标头下的所有单元格。我的代码在下面。
我正在尝试像这样在正确标题下过滤名称。对于标题“ A”,如果ContactName以“ A”开头,则该名称将显示在标题“ A”下,依此类推。
在CellforRowat
下,我一直在尝试修改下面的行,但没有成功。
let regionContacts = filteredContacts.map{$0.ContactName? == region}
struct userInfo {
var ContactName : String?
}
var contacts:[userInfo]?
var filteredContacts = [userInfo]()
var Contacts:userInfo?
var contactSectionHeader: [String] = ["A" ,"B" , "C" , "D" , "E" ,"F","G" ,"H" , "I" , "J" , "K" ,"L","M" ,"N" , "O" , "P" , "Q" ,"R","S" ,"T" , "U" , "V" , "W" ,"X", "Y","Z"]
func numberOfSections(in tableView: UITableView) -> Int {
return contactSectionHeader.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//let sorted = contacts?.sorted(by: {return $0.ContactName! < $1.ContactName!})
let (alpha) = contactSectionHeader[section]
let count = alpha
if searchController.isActive && searchController.searchBar.text != "" {
//return filteredContacts.sorted(by: {return $0.ContactName! < $1.ContactName!}).count
return filteredContacts.count
}
else {
// return (contacts?.sorted(by: {return $0.ContactName! < $1.ContactName!}).count)!
//return contacts?.sort(by: {$0.ContactName! < $1.ContactName!})
return contacts?.count ?? 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ECTableViewCell
let (region) = contactSectionHeader[indexPath.section]
let currentContact: userInfo
if searchController.isActive && searchController.searchBar.text != "" {
let regionContacts = filteredContacts.map{$0.ContactName? == region}
//let regionContacts = filteredContacts.sorted(by: {return $0.ContactName! < $1.ContactName!})
currentContact = regionContacts[indexPath.row]
}
else {
let regionContacts = contacts?.filter{$0.ContactName! == region}
// let regionContacts = contacts?.sort{return $0.ContactName! < $1.ContactName!}
currentContact = (contacts?[indexPath.row])!
}