代码之家  ›  专栏  ›  技术社区  ›  Hortitude

无法在Swift中设置IndexPath行

  •  0
  • Hortitude  · 技术社区  · 6 年前

    下面的代码产生了一个运行时错误,我不知道是如何产生的。有什么想法吗?

        var foo: IndexPath
        foo = IndexPath()
        foo.row = 1
    
        var i = 0
    

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  1
  •   Jawad Ali    6 年前

    swift中有一个先决条件来访问 indexpath

    /// The section of this index path, when used with `UITableView`.
    ///
    /// - precondition: The index path must have exactly two elements.
    public var section: Int
    
    /// The row of this index path, when used with `UITableView`.
    ///
    /// - precondition: The index path must have exactly two elements.
    public var row: Int
    

    enter image description here

    如果要访问或更改行,需要使用row和section初始化indexPath

     var foo: IndexPath
     foo = IndexPath(row: 0, section: 0)
     foo.row = 1 // return foo (1,0)