I know the code is not the prettiest, I'm still learning ways to condense my code and reduce the amount of lines needed for things such as the way I have for sorting my list. However, I do need help trying to figure out why this list is not showing a highlight over whatever row is selected. For reference, I put a print statement to verify taps are being registered on the rows, and it is working fine.
Some further info: the "onDelete" action for the row is deleting the wrong row. Perhaps this has something to do with the selection? Any advice would be great.
//
// TableView.swift
// TableDemo (iOS)
//
// Created by Kyle Carroll on 7/20/21.
//
//TODO: Delete at "index set" is deleting wrong row
//TODO: Row selection not working
import SwiftUI
struct TableView: View {
@StateObject var bookStore : BookStore = BookStore(books:bookData)
@State var sortBy: String = ""
@State var IDbuttonStatus: IDButtonStatus = .firstPress
@State var titleButtonStatus: TitleButtonStatus = .firstPress
@State var authorButtonStatus: AuthorButtonStatus = .firstPress
@State var pagesButtonStatus: PagesButtonStatus = .firstPress
enum IDButtonStatus {
case secondPress
case firstPress
var nextStatus: IDButtonStatus {
switch self {
case .firstPress: return .secondPress
case .secondPress: return .firstPress
}
}
var order: String {
switch self {
case .firstPress: return "id2"
case .secondPress: return "id"
}
}
}
enum TitleButtonStatus {
case firstPress
case secondPress
var nextStatus: TitleButtonStatus {
switch self {
case .firstPress: return .secondPress
case .secondPress: return .firstPress
}
}
var order: String {
switch self {
case .firstPress: return "title2"
case .secondPress: return "title"
}
}
}
enum AuthorButtonStatus {
case firstPress
case secondPress
var nextStatus: AuthorButtonStatus {
switch self {
case .firstPress: return .secondPress
case .secondPress: return .firstPress
}
}
var order: String {
switch self {
case .firstPress: return "author2"
case .secondPress: return "author"
}
}
}
enum PagesButtonStatus {
case firstPress
case secondPress
var nextStatus: PagesButtonStatus {
switch self {
case .firstPress: return .secondPress
case .secondPress: return .firstPress
}
}
var order: String {
switch self {
case .firstPress: return "pages2"
case .secondPress: return "pages1"
}
}
}
var body: some View {
VStack {
HStack(alignment: .center, spacing: nil) {
HStack {
Text("ID")
Button {
IDbuttonStatus = IDbuttonStatus.nextStatus
self.sortBy = IDbuttonStatus.order
} label: {
Image(systemName: "chevron.up.chevron.down")
.foregroundColor(Color.black)
}
}
.frame(maxWidth: 50, alignment: .leading)
Divider()
HStack {
Text("Title")
Button {
titleButtonStatus = titleButtonStatus.nextStatus
self.sortBy = titleButtonStatus.order
} label: {
Image(systemName: "chevron.up.chevron.down")
.foregroundColor(Color.black)
}
}
.frame(maxWidth: 200, alignment: .leading)
Divider()
HStack {
Text("Author")
Button {
authorButtonStatus = authorButtonStatus.nextStatus
self.sortBy = authorButtonStatus.order
} label: {
Image(systemName: "chevron.up.chevron.down")
.foregroundColor(Color.black)
}
}
.frame(maxWidth: 200, alignment: .leading)
Divider()
HStack {
Text("Pages")
Button {
pagesButtonStatus = pagesButtonStatus.nextStatus
self.sortBy = pagesButtonStatus.order
} label: {
Image(systemName: "chevron.up.chevron.down")
.foregroundColor(Color.black)
}
}
.frame(maxWidth: 75, alignment: .leading)
}
.frame(maxWidth: .infinity, maxHeight: 40, alignment: .leading)
.padding(.leading, 20)
List {
switch self.sortBy {
case "title":
ForEach (bookStore.books.sorted {$0.title < $1.title}) { book in
ListCell(book: book)
}
.onDelete(perform: deleteItems)
case "title2":
ForEach (bookStore.books.sorted {$0.title > $1.title}) { book in
ListCell(book: book)
}
case "id":
ForEach (bookStore.books.sorted {$0.id < $1.id}) { book in
ListCell(book: book)
}
case "id2":
ForEach (bookStore.books.sorted {$0.id > $1.id}) { book in
ListCell(book: book)
}
case "author":
ForEach (bookStore.books.sorted {$0.authorsort < $1.authorsort}) { book in
ListCell(book: book)
}
case "author2":
ForEach (bookStore.books.sorted {$0.authorsort > $1.authorsort}) { book in
ListCell(book: book)
}
case "pages":
ForEach (bookStore.books.sorted {$0.pages < $1.pages}) { book in
ListCell(book: book)
}
case "pages2":
ForEach (bookStore.books.sorted {$0.pages > $1.pages}) { book in
ListCell(book: book)
}
default:
ForEach (bookStore.books.sorted {$0.id < $1.id}) { book in
ListCell(book: book)
}
}
}
.listStyle(PlainListStyle())
.frame(maxWidth: .infinity, alignment: .leading)
}
}
func deleteItems(at offsets: IndexSet) {
bookStore.books.remove(atOffsets: offsets)
}
}
struct ListCell: View {
var book : Book
var body: some View {
HStack(alignment: .center, spacing: nil) {
Text("\(book.id)")
.frame(maxWidth: 50, alignment: .leading)
Divider()
Text(book.title)
.frame(maxWidth: 200, alignment: .leading)
Divider()
Text(book.author)
.frame(maxWidth: 200, alignment: .leading)
Divider()
Text("\(book.pages)")
.frame(maxWidth: 75, alignment: .leading)
}
.onTapGesture {
print(book.title)
}
}
}
struct TableView_Previews: PreviewProvider {
static var previews: some View {
if #available(iOS 15.0, *) {
TableView()
.previewInterfaceOrientation(.landscapeLeft)
} else {
// Fallback on earlier versions
}
}
}
