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

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

    var i = 0

ios - 无法在Swift中设置IndexPath行-LMLPHP

最佳答案

迅速有一个先决条件才能访问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

ios - 无法在Swift中设置IndexPath行-LMLPHP

如果要访问或更改行,则需要使用行和节初始化indexPath
 var foo: IndexPath
 foo = IndexPath(row: 0, section: 0)
 foo.row = 1 // return foo (1,0)

关于ios - 无法在Swift中设置IndexPath行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62452415/

10-15 08:08