想要索引表视图并引用回源数组
Want indexed tableview with reference back to source array
在 iOS/Swift 中,我根据客户端 class 中的 clientName 属性 创建了一个索引 "client" UITableView。我创建了一本以 A 到 Z 为部分的字典。索引表视图效果很好。但是,我试图找出一种方法来确定它在原始源数组中的哪一行,当用户选择一行时。我正在考虑构建某种类型的交叉引用数组,除了字典最终排序以匹配部分,所以我不知道哪个 section/row 组合匹配哪个原始数组条目。是否有处理此问题的通用方法?
试图澄清...
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 { [=10=] < }
}
现在,当用户点击表视图中的一行时,我可以获得部分和行 (indexPath)。但是,假设可能有重复的名称,我如何确定 clients 数组中的哪一行是匹配的?有什么方法可以动态创建映射到源数组中行的索引 section/row 的交叉引用吗?我打算在构建字典时尝试这样做,除了字典在之后排序,所以没有任何匹配。也许我应该以某种方式包括源行号 in/with 字典??
这是表格视图代码:
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)
}
我明白了。我没有意识到(直到我最近尝试过)您可以在字典中嵌套任何 class 的数组。当我更改我的字典以在其中嵌套我的客户数组时,一切都解决了。我改变了我的功能,如下所示。
func createClientDict() {
// Declared for view controller. Re-initialized here.
clientDict = [String: [Client]]()
clientSectionTitles = [String]()
clients.sortInPlace ({ [=10=].lastName < .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 { [=10=] < }
}
然而,这一行是解决问题的关键:
let clientDict = [String: [Client]]()
在 iOS/Swift 中,我根据客户端 class 中的 clientName 属性 创建了一个索引 "client" UITableView。我创建了一本以 A 到 Z 为部分的字典。索引表视图效果很好。但是,我试图找出一种方法来确定它在原始源数组中的哪一行,当用户选择一行时。我正在考虑构建某种类型的交叉引用数组,除了字典最终排序以匹配部分,所以我不知道哪个 section/row 组合匹配哪个原始数组条目。是否有处理此问题的通用方法?
试图澄清...
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 { [=10=] < }
}
现在,当用户点击表视图中的一行时,我可以获得部分和行 (indexPath)。但是,假设可能有重复的名称,我如何确定 clients 数组中的哪一行是匹配的?有什么方法可以动态创建映射到源数组中行的索引 section/row 的交叉引用吗?我打算在构建字典时尝试这样做,除了字典在之后排序,所以没有任何匹配。也许我应该以某种方式包括源行号 in/with 字典??
这是表格视图代码:
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)
}
我明白了。我没有意识到(直到我最近尝试过)您可以在字典中嵌套任何 class 的数组。当我更改我的字典以在其中嵌套我的客户数组时,一切都解决了。我改变了我的功能,如下所示。
func createClientDict() {
// Declared for view controller. Re-initialized here.
clientDict = [String: [Client]]()
clientSectionTitles = [String]()
clients.sortInPlace ({ [=10=].lastName < .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 { [=10=] < }
}
然而,这一行是解决问题的关键:
let clientDict = [String: [Client]]()