我有一个包含完整字母的部分索引 – 但是,我的表中的部分只包含几个字母(例如C,F,N,U).
我想修改sectionForSectionIndexTitle以在滑动手指超过部分索引时正确显示右侧部分.我仍然不确定如何使用sections.append …
谢谢你的帮助.
这是我的代码的样子:
Section.swift
struct Section { var heading : String var items : [String] init(title: String,objects : [String]) { heading = title items = objects } }
ViewController.swift
func updateSections(index : Int) { switch index { case 0: sections.append(Section(title: "C",objects: ["Czech Republic"])) sections.append(Section(title: "F",objects: ["France"])) sections.append(Section(title: "N",objects: ["norway"])) sections.append(Section(title: "U",objects: ["USA"])) default: break } tableViewOutlet.reloadData() } let arrIndexSection = ["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 sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { if segmentedControlOutlet.selectedSegmentIndex == 0 { return arrIndexSection } else { return [""] } } func tableView(tableView: UITableView,sectionForSectionIndexTitle title: String,atIndex index: Int) -> Int { let temp = arrIndexSection as NSArray return temp.indexOfObject(title) } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return sections.count } func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int { let section = sections[section] return section.items.count } func tableView(tableView: UITableView,titleForHeaderInSection section: Int) -> String? { return sections[section].heading }
解决方法
尝试在项目中使用以下代码:
func tableView(tableView: UITableView,atIndex index: Int) -> Int { var tempIndex:Int = 0 for str in arrIndexSection { if str == title { return tempIndex } tempIndex += 1 } return 0 }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。