SwiftUI Table with TextField becomes unresponsive as entries increase

I am trying to implement a simple Table with a TextField that makes it possible to edit the data.

Below is a very simple example where I am trying to do this. It works, but it becomes very unresponsive as the number of people increases.

The example as it is, has 1,000 people. It is very slow when a TextField gets focus and is used to edit something. Strangely, it also gets very slow when you scroll through an entry that has been edited. It crashes completely when 10,000 people is used.

I found this similar question on the web, but it is not answered.

What should I do to implement an editable Table like this?

struct ContentView: View {
  struct Person: Identifiable {
    var givenName: String
    var familyName: String
    let id = UUID()
  }
  
  @State private var people = [Person].init(repeating: Person(givenName: "Name", familyName: "Family"), count: 1000)
  
  var body: some View {
    Table($people) {
      TableColumn("Given Name") { $person in TextField("Name", text: $person.givenName) }
      TableColumn("Family Name") { $person in TextField("Family Name", text: $person.familyName) }
      TableColumn("Full Name") { $person in Text("\(person.givenName) \(person.familyName)")}
    }
  }
}
Post not yet marked as solved Up vote post of oreman Down vote post of oreman
1.4k views

Replies

Hi, did you solve this? My guess is ( I didn't need it yet so far, but know I have to start about this next week ), but can't you make all the fields a Text and do a check if it is a row/column that should be ( by check double click or so ) edited replace the row with TextField's and change it back to Text. I think that you to have .modify one of the other to match each other visually.

Beside that the var is used in all the rows and the same and is not usable to edit or change one people item.

But if you found a solution then.... let us/me know.

  • Sorry, did not manage to find a solution.

Add a Comment