I'm trying to create dynamic, alphabetical sections for a list, similar to Contacts or Music. (Ultimately, I'd like to have the alphabetical sidebar, but that's something I'll add later.)
The challenge is that I'm dealing with lists of book titles, and I can't always be sure the first character of the title is going to be a character from the English alphabet, so I'm trying to derive my list of firstCharacter
s for the section headers from the actual titles in the library.
That part I more or less have worked out, it's presenting the filtered dynamic list within the section that I'm not quite getting:
var firstCharacters: [Character] {
var firstCharacters = [Character]()
for book in books.sorted(by: {$0.title < $1.title}) {
if let character = book.title.first, !firstCharacters.contains(character) {
firstCharacters.append(character)
}
}
return firstCharacters
}
var body: some View {
VStack {
ForEach(firstCharacters) { character in
Section(header: Text(String(character))) {
List(books.filter({$0.title.first == character }).sorted(by: {$0.title < $1.title})) { book in
BookRowView(book: book, index: nil)
}
}
}
.listStyle(InsetListStyle())
}
}
This almost does what I want, except that each section is its own little scroll view only a single line of text high, rather than the whole thing being a continuous, sectioned list.