Search code examples
ioslistswiftuirowmove

SwiftUI List - disable moving of particular rows?


In a SwiftUI List that is in Edit mode, each row has a handle at its trailing edge so that the row can be moved up or down in the sequence of rows. In UIKit there is an instance method tableView(_:canMoveRowAt:) which specifies which rows contains those handles and which do not.

I'm seeking the equivalent in SwiftUI. Any ideas?


Solution

  • You need to use .moveDisabled(condition) modifier, like in below example

        ForEach(items, id: \.self) { item in
            Text(item)
              .moveDisabled(item == "nonmovable item")    // << conditional !!
        }