在我的表视图中,有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")
}
}发布于 2015-04-15 00:05:00
我建议使用元组:
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
}(下划线是一个“不关心”占位符,与该位置中的任何值匹配。)
这样,每一种情况的语法都很好,而且易于阅读。
发布于 2015-04-14 23:56:23
您必须使用indexPath.row返回其行索引,使用indexPath.section获取其节,但两者都是Int的;请小心。
发布于 2015-04-14 23:58:35
节可以引用indexPath.section,节内的行可以引用indexPath.row
祝好运
https://stackoverflow.com/questions/29639332
复制相似问题