0

According to the docs, this should be as easy as this:

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

What I have is a very complicated tableview ( I need to make my changes in cellForRow, anything else would require too large of a refactor, not to mention it wouldn't be very safe.)

Several sections - the section I am working on right now has different custom cells. I handle all the difference cells with an an enum. For the case I am working on right now, my data source (extensive and impossible to refactor right now) only accounts for this type of cell (not all the others in the section). of course I tried

[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. 

How can I start the indexPath over again in the same section? so my new group of cells can respond to the indexPath of the data source and all other methods.

I was thinking that indexPath.dropFirst(1) seemed ideal, but it is not working. I an Exec - unwrapping a value found to be nil error.

valeriana
  • 161
  • 1
  • 16
  • 1
    How about `let newIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)` ? – vacawama May 27 '20 at 01:01
  • You saved me!!! @vacawama – valeriana May 27 '20 at 13:36
  • For anyone seeing this in the future. the other option for this is to make every time you need new indexing starting from 0, just make it its own section. If you can, if not, this will get you where you need to go . – valeriana May 27 '20 at 13:39
  • @vacawama add this as an answer so I can make it the accepted answer. Thanks so much! – valeriana May 27 '20 at 13:39

1 Answers1

1

If you want the indexPath to move back by 1, use the .row and .section properties get the components and then use the IndexPath(row:section:) initializer to create the newIndexPath:

let newIndexPath = IndexPath(row: indexPath.row - 1, section: indexPath.section)
vacawama
  • 150,663
  • 30
  • 266
  • 294