如何引用 NSIndexPath?

How to refer to an NSIndexPath?

在我的table视图中,有 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")

        }           

 }

您必须使用 indexPath.row 到 return 它的行索引和 indexPath.section 来获取它的部分,但两者都是 Int;小心。

您可以参考 indexPath.section 部分和 indexPath.row 部分内的行

祝你好运

我建议使用元组:

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
}

(下划线是 "don't care" 占位符,匹配该位置的任何值。)

这样每个案例的语法都很好,也很容易阅读。