【问题标题】:drop the first element of the indexpath-> indexpath - 1删除 indexpath-> indexpath - 1 的第一个元素
【发布时间】:2020-09-13 20:55:51
【问题描述】:

根据文档,这应该像这样简单:

     let newIndexPath = indexPath.dropFirst(1). //This is  not working though. 

我拥有的是一个非常复杂的表格视图(我需要在 cellForRow 中进行更改,其他任何东西都需要进行太大的重构,更不用说它不是很安全。)

几个部分 - 我现在正在处理的部分有不同的自定义单元格。我用枚举处理所有差异单元格。对于我现在正在处理的案例,我的数据源(广泛且目前无法重构)仅考虑这种类型的单元格(不是本节中的所有其他单元格)。当然我试过了

[indexPath.row - 1]//And this works!!  But is is not gonna fly in code review to add this as an index path to all my methods to configure this group of cells. 

如何在同一部分重新启动 indexPath?所以我的新组单元格可以响应数据源的 indexPath 和所有其他方法。

我认为 indexPath.dropFirst(1) 似乎很理想,但它不起作用。我是一个 Exec - 展开一个发现为 nil 错误的值。

【问题讨论】:

  • let newIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section) 怎么样?
  • 你救了我!!! @vacawama
  • 对于将来看到此内容的任何人。另一个选择是每次需要从 0 开始的新索引时,只需将其设为自己的部分。如果可以,如果不能,这将带你去你需要去的地方。
  • @vacawama 将此添加为答案,以便我将其设为可接受的答案。非常感谢!

标签: swift uitableview tableview indexpath


【解决方案1】:

如果您希望indexPath 向后移动1,请使用.row.section 属性获取组件,然后使用IndexPath(row:section:) 初始化程序创建newIndexPath

let newIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多