nac*_*tel 1 xcode uitableview tableview nsindexpath swift
在我的表视图中,有4个部分,第一部分有3行,第二部分有2行,第三部分有2行,第四部分有1行.
当按下时如何引用这些行?这是我到目前为止使用的代码.请帮忙
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("\(indexPath)")
switch indexPath {
case 0.1, 0.2, 0.3:
println("Row 1, 2, or 3 in section 1")
case 1.1:
println("Row 1 in section 2")
case 1.2:
println("Row 2 in section 2")
case 2.2:
println("Row 2 in section 3")
case 3.1:
println("Row 1 in section 4")
default:
println("Default")
}
}
Run Code Online (Sandbox Code Playgroud)
我建议使用一个元组:
switch (indexPath.section, indexPath.row)
{
case (0,1), (0,2), (0,3):
print("Row 1, 2, or 3 in section 1")
case (1,1):
print("Row 1 in section 2")
case (4, _):
print("any row in section 5")
default:
break
}
Run Code Online (Sandbox Code Playgroud)
(Underscore是一个"不关心"占位符,匹配该位置的任何值.)
这样,每个案例的语法都很好,易于阅读.