删除 indexpath-> indexpath - 1 的第一个元素。

问题描述 投票:0回答:1

根据文档,这应该是很简单的。

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

我有一个非常复杂的tableview(我需要在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错误。

swift uitableview tableview indexpath
1个回答
1
投票

如果你想要的是 indexPath 倒退 1,使用 .row.section 属性获取组件,然后使用 IndexPath(row:section:) 初始化器来创建 newIndexPath:

let newIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)
© www.soinside.com 2019 - 2024. All rights reserved.