在iOS / Swift中,我基于Client类中的clientName属性创建了一个索引的“客户端”UITableView。我用A到Z创建了一个字典作为部分。索引的tableview效果很好。但是,当用户选择一行时,我正试图找出一种方法来确定它在原始源数组中的哪一行。我正在考虑构建某种类型的交叉引用数组,除了字典最终排序以匹配部分,所以我不知道哪个部分/行组合匹配哪个原始数组条目。处理这个问题有一个共同的方法吗?
试图澄清......
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中点击一行时,我可以获得section和row(indexPath)。但是,假设可能存在重复的名称,如何确定clients数组中的哪一行匹配?是否有某种方法可以动态创建映射到源数组中的行的索引节/行的交叉引用?我打算在构建字典时尝试这样做,除了字典之后排序,所以没有任何东西可以匹配。也许我应该以某种方式在字典中包含源行号?
这是tableview代码:
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)
}
答案 0 :(得分:0)
我明白了。我没有意识到(直到我最近尝试过)你可以在字典中嵌套任何类的数组。当我更改字典以使我的客户端数组嵌套在其中时,所有内容都已解决。我改变了我的功能,如下所示。
func createClientDict() {
// Declared for view controller. Re-initialized here.
clientDict = [String: [Client]]()
clientSectionTitles = [String]()
clients.sortInPlace ({ $0.lastName < $1.lastName })
for c in clients {
let clientName = "\(c.lastName), \(c.firstName)"
// Get the first letter of the name and build the dictionary
let clientKey = clientName!.substringToIndex(clientName!.startIndex.advancedBy(1))
if var clientValues = clientDict[clientKey] {
clientValues.append(c)
clientDict[clientKey] = clientValues
} else {
clientDict[clientKey] = [c]
}
}
// Get the section titles from the dictionary's keys and sort them in ascending order
clientSectionTitles = [String](clientDict.keys)
clientSectionTitles = clientSectionTitles.sort { $0 < $1 }
}
然而,这条线是解决方案的关键:
let clientDict = [String: [Client]]()