将 (UITableView numberOfRowsinSection switch) 更改为更小、更好的代码?
Change (UITableView numberOfRowsinSection switch) into smaller, nicer code?
我在 TableView 中填充 Persons 列表,按字母顺序分成几个部分。使用 Switch case 可以完成这项工作,但我试图找出一些 "loopier" 左右的更微妙的代码,但没有成功。
有没有办法改变这个:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return the number of rows for index letters
// example: for A there are 2 contacts Alex and Andrea
// so rows in section for A are 2
switch (determineKeyList()[section]) {
case "A": return determineValueCountForKey("A")
case "B": return determineValueCountForKey("B")
case "C": return determineValueCountForKey("C")
case "D": return determineValueCountForKey("D")
case "E": return determineValueCountForKey("E")
// every alphabet letter follows..
default: return 0
}
}
为数据库中的每个案例制作更好的东西。另外,如果我的列表中没有人怎么办,比如以 "H" 开头的名字?默认 return 0?或者我需要额外的检查来跳过这封信?
谢谢!
以下喜欢的内容应该有效:
if (section > 5) { // E is the 5th letter
return 0;
}
return determineValueCountForKey(determineKeyList()[section]);
除了当前的 if 条件,您还可以检查返回的字母是否大于 'H'
。
我在 TableView 中填充 Persons 列表,按字母顺序分成几个部分。使用 Switch case 可以完成这项工作,但我试图找出一些 "loopier" 左右的更微妙的代码,但没有成功。
有没有办法改变这个:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return the number of rows for index letters
// example: for A there are 2 contacts Alex and Andrea
// so rows in section for A are 2
switch (determineKeyList()[section]) {
case "A": return determineValueCountForKey("A")
case "B": return determineValueCountForKey("B")
case "C": return determineValueCountForKey("C")
case "D": return determineValueCountForKey("D")
case "E": return determineValueCountForKey("E")
// every alphabet letter follows..
default: return 0
}
}
为数据库中的每个案例制作更好的东西。另外,如果我的列表中没有人怎么办,比如以 "H" 开头的名字?默认 return 0?或者我需要额外的检查来跳过这封信?
谢谢!
以下喜欢的内容应该有效:
if (section > 5) { // E is the 5th letter
return 0;
}
return determineValueCountForKey(determineKeyList()[section]);
除了当前的 if 条件,您还可以检查返回的字母是否大于 'H'
。