【问题标题】:Cannot set IndexPath row in Swift无法在 Swift 中设置 IndexPath 行
【发布时间】:2020-10-08 15:42:04
【问题描述】:

以下代码会产生运行时错误,我不知道是怎么回事。有什么想法吗?

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

    var i = 0

【问题讨论】:

    标签: ios swift xcode uitableview uiviewcontroller


    【解决方案1】:

    快速访问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
    

    如果要访问或更改行,则需要使用行和节来初始化 indexPath

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

    【讨论】:

    • 您是如何进入 IndexPath 源的?当我尝试介入时,我没有得到你得到的漂亮的源视图。
    • 当我这样做时,我只得到汇编代码:libswiftUIKit.dylib`merged(UIKit 中的扩展):Foundation.IndexPath.section.setter:Swift.Int:0x1022b22c0 :pushq % rbp 0x1022b22c1 : movq %rsp, %rbp
    猜你喜欢
    • 2021-04-04
    • 2018-06-10
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2018-11-28
    • 1970-01-01
    • 2015-07-25
    相关资源
    最近更新 更多