在 iOS/Swift 中,我基于 Client 类中的 clientName 属性创建了一个索引的“客户端”UITableView。我创建了一个以 A 到 Z 作为部分的字典。索引表视图效果很好。但是,我试图找出一种方法来确定当用户选择一行时它在原始源数组中的哪一行。我正在考虑构建某种类型的交叉引用数组,除了字典最终排序以匹配部分,所以我不知道哪个部分/行组合匹配哪个原始数组条目。是否有处理此问题的通用方法?
为了澄清...
class Client {
var clientId : Int!
var firstName : String!
var lastName : String!
var email : String!
var phone : String!
...
init() {
}
}
var clients: [Client] = []
// clients array loaded from web service
...
// Create dictionary to be source for indexed tableview
func createClientDict() {
clientDict = [String: [String]]()
clientSectionTitles = [String]()
var clientNames:[String] = []
for i in 0..<clients.count {
let client = clients[i]
let clientName = "\(client.lastName), \(client.firstName)"
clientNames.append(clientName)
}
for name in clientNames {
var client: Client = Client()
// Get the first letter of the name and build the dictionary
let clientKey = name.substringToIndex(name.startIndex.advancedBy(1))
if var clientValues = clientDict[clientKey] {
clientValues.append(name)
clientDict[clientKey] = clientValues
} else {
clientDict[clientKey] = [name]
}
}
// Get the section titles from the dictionary's keys and sort them in ascending order
clientSectionTitles = [String](clientDict.keys)
clientSectionTitles = clientSectionTitles.sort { $0 < $1 }
}
现在,当用户在 tableview 中点击一行时,我可以获得部分和行(indexPath)。但是,假设可能有重复的名称,我如何确定 clients 数组中的哪一行是匹配的?是否有某种方法可以动态创建映射到源数组中行的索引部分/行的交叉引用?我打算在构建字典时尝试这样做,除了字典被排序之后,所以没有任何匹配。也许我应该以某种方式将源行号包含在字典中/与字典中?
这是表格视图代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ClientCell
let clientKey = clientSectionTitles[indexPath.section]
if let clientValues = clientDict[clientKey] {
cell.clientName.text = clientValues[indexPath.row]
}
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return clientSectionTitles.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let clientKey = clientSectionTitles[section]
if let clientValues = clientDict[clientKey] {
return clientValues.count
}
return 0
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return clientSectionTitles[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return clientIndexTitles
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
guard let index = clientSectionTitles.indexOf(title) else {
return -1
}
return index
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.contentView.backgroundColor = UIColor ( red: 0.0, green: 0.3294, blue: 0.6392, alpha: 1.0 )
headerView.textLabel?.textColor = UIColor.greenColor()
headerView.textLabel?.font = UIFont(name: "Noteworthy-bold", size: 15.0)
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndex = indexPath
// In the following prepare for segue, I need to somehow use the selected indexpath to find the correct entry
// in the clients array and pass it along.
performSegueWithIdentifier("clientDetailSegue", sender: self)
}