gpt4 book ai didi

ios - 表 TableView 中的字母部分

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:02 24 4
gpt4 key购买 nike

我有一个按字母顺序排序的名字列表,现在我想在表格 View 中显示这些名字。我正在努力为每个字母对这些名称进行分组。

我的代码是这样的:

  class ContactTableViewController: UITableViewController {

var contactArray = ["Alan Douglas", "Bob", "Bethany Webster", "Casey Fleser", "Daniel Huckaby", "Michael Morrison"]
var contactSection = [String]()
var contactDictionary = [String : [String]]()

override func viewDidLoad() {
super.viewDidLoad()

generateWordsDict()

let nav = self.navigationController?.navigationBar
nav?.tintColor = UIColor(red: 16.0, green: 18.0, blue: 34.0, alpha: 1.0)
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func generateWordsDict(){
for contact in contactArray {

let key = "\(contact[contact.startIndex])"
let lower = key.lowercased()

if var contactValue = contactDictionary[lower]
{
contactValue.append(contact)
}else{
contactDictionary[lower] = [contact]
}
}
contactSection = [String](contactDictionary.keys)
contactSection = contactSection.sorted()
}
// MARK: - Table view data source

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

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return contactSection[section]
}

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

let contactKey = contactSection[section]
if let contactValue = contactDictionary[contactKey]{
return contactValue.count
}
return 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellContact", for: indexPath)

let cotactkey = contactSection[indexPath.section]

if let contactValue = contactDictionary[cotactkey.lowercased()] {

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

cell.textLabel?.textColor = UIColor.white
}
return cell
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return contactSection
}

override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
guard let index = contactSection.index(of: title) else {
return -1
}
return index
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
}
}

一切都很好,除了分组使我的表格 View 最终像这样:

enter image description here

我想做这样的事情:

enter image description here

但我不知道如何实现:

How to adjust the color of the letters? how to make a list of names to display correctly?

最佳答案

cellForRowAt 中,您需要使用 contactValue 而不是 contactArray

换行

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

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

此外,您还需要实现 titleForHeaderInSection 而不是 titleForFooterInSection

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return contactSection[section].uppercased()
}

要将章节标题颜色更改为白色,请执行willDisplayFooterView 方法。

override func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
if let view = view as? UITableViewHeaderFooterView {
view.textLabel?.textColor = .white
}
}

tableViewsectionIndexBackgroundColor 属性更改为您想要的颜色,而不是 white 颜色。

self.tableView.sectionIndexBackgroundColor = .black

关于ios - 表 TableView 中的字母部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43344760/

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