Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't select same row twice in SwiftUI

Tags:

I have a navigation list with multiple sections and rows. I select a row foo, it navigates to the view I want. However, when I go back to the root view, I can't select row foo. I tap row foo and nothing happens.

I tap row bar and that row sends me to its view. Back to the root view. Then I can't select row bar, but now row foo works.

Is this a bug in SwiftUI or designed behavior? Is there something I need to do to reset views when I leave them?

NavigationView {
            List {
Section(header: shoppingListData.lastItemSection.sectionHeader, footer: shoppingListData.lastItemSection.sectionFooter) {
            ForEach(0..<shoppingListData.lastItemSection.sectionRows.count) { index in
                ShoppingItemRow(shoppingListData: self.shoppingListData,
                                rowItem: self.shoppingListData.lastItemSection.sectionRows[index])
            }
        }
}
}

Here is another case with the same problem. I can only select the picker row of the form once. If I go back to the root view and then back again to this view, I can select the picker again.

If I set the pickerStyle to SegmentedPickerStyle(), I can select it multiple times.

struct ShoppingItemPage: View {
    @ObservedObject var shoppingListData: ShoppingListData
    @ObservedObject var shoppingItem: ShoppingItems
    var body: some View {
        Form {
            Section(header: Text("Packages")) {
                HStack {
                    Text("Quantity (\(shoppingItem.myUnit.myName))")

                    TextField("Quantity (\(shoppingItem.myUnit.myName))", value: $shoppingItem.stdQty, formatter: basicFormat)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .keyboardType(.numbersAndPunctuation)

                    Toggle("Need", isOn: $shoppingItem.needed)
                }
                HStack {
                    Text("Item Name")
                    TextField("Item Name", text: $shoppingItem.myName, onEditingChanged: { (a) in
                        self.shoppingItem.modified()
                    }) {
                        self.shoppingItem.modified()
                    }.textFieldStyle(RoundedBorderTextFieldStyle())
                }


                Picker(selection: $shoppingItem.urgency, label: Text("Urgency")) {
                    ForEach(Ledgers.ReceiptUrgency.list(), id: \.rawValue) { urgency in
                        Text(urgency.description()).tag(urgency)
                    }
                }                
            }
        }.navigationBarTitle(Text(shoppingItem.myName))
    }
}

Running XCode Version 11.2.1 (11B500) and iOS 13.3 beta.

Adding ShoppingItemRow for more information

struct ShoppingItemRow: View {

    @ObservedObject var shoppingListData: ShoppingListData
    @ObservedObject var rowItem: ShoppingItems

    var id: UUID {
        return rowItem.uuidKey
    }

    var body: some View {
        NavigationLink(destination: ShoppingItemPage(shoppingListData: shoppingListData, shoppingItem: rowItem)) {
            HStack(alignment: .center) {
                VStack(alignment: .leading)  {
                    rowName
                    rowDescription
                    rowPremiumDescription
                }
                Spacer()
                VStack(alignment: .trailing) {
                    rowPrice
                    rowPremium
                }
            }.padding(3)
            }.background(premiumColor)
    }

    var rowName: Text {
        if let msp = rowItem.minStorePackage {
            return Text(msp.brandName).font(.body).fontWeight(.bold)
        }
        // fall through
        return Text(rowItem.myName).font(.body).fontWeight(.bold)
    }

    var rowPrice: Text {
        if let msp = rowItem.minStorePackage {
            let dq = msp.defQty
            let pr = msp.pkgCost(pkgQty: dq)
            return Text(pr.cash()).font(.body)
        } else if let mp = rowItem.minPackage {
            let dq = mp.defQty
            let pr = mp.pkgCost(pkgQty: dq)
            return Text(pr.cash()).font(.body)
        } else {
            return Text("rowPrice Test")
            // return Text("0").hidden() as! Text
        }
    }

    var rowPremium: Text? {
        if let msp = rowItem.minStorePackage {
            let dq = msp.defQty
            let pc = msp.premiumCents(pkgQty: dq)
            if pc == 0 {
                return Text("0").hidden() as? Text
            } else {
                return Text(pc.cash()).font(.caption)
            }
        } else {
            return Text("0").hidden() as? Text
        }
    }

    var rowDescription: Text? {
        if let msp = rowItem.minStorePackage {
            let dq = msp.defQty
            let unitText: String
            if msp.pkgInteger {
                if dq == 1 {
                    unitText = "\(msp.pkgSize.basicString()) \(rowItem.myUnit.myName)"
                } else {
                    unitText = "\(dq.basicString()) x [\(msp.pkgSize.basicString()) \(rowItem.myUnit.myName)]"
                }
            } else {
                unitText = "\((dq * msp.pkgSize).basicString()) \(rowItem.myUnit.myName)"
            }
            let thisText = "\(unitText) \(msp.costX()) (\(msp.stdPrice.cash())/\(rowItem.myUnit.myName))"
            return Text(thisText).font(.caption)
        } else {
            return Text("").hidden() as? Text
        }
    }

    var rowPremiumDescription: Text? {
        if let msp = rowItem.minStorePackage {
            let dq = msp.defQty
            let premium = msp.premiumCents(pkgQty: dq)
            if premium == 0 {
                return Text("Minimum price at \(shoppingListData.dataStack.currentReceipt.myStore!.longName).").font(.caption)
            } else {
                let mp = rowItem.minPackage!
                return Text("\(premium.cash()) cheaper at \(mp.myStore.longName)").font(.caption)
            }
        } else if let mp = rowItem.minPackage {
            let dq = mp.defQty
            let pc = "Minimum price \(mp.pkgCost(pkgQty: dq).cash()) (\(mp.stdPrice.cash()) \(rowItem.myUnit.myName)) at "
            let storeName = mp.myStore.longName
            return Text("\(pc)\(storeName)").font(.caption)
        } else {
            return Text("").hidden() as? Text
        }
    }

    var premiumColor: Color {
        if let msp = rowItem.minStorePackage {
            let dq = msp.defQty
            let pc = msp.premiumCents(pkgQty: dq)
            if pc == 0 {
                return Color.yellow
            } else {
                return Color.clear
            }
        } else {
            return Color.clear
        }
    }

}
like image 576
adamek Avatar asked Nov 27 '19 00:11

adamek


2 Answers

The bug is fixed by Apple in iOS 13.3 beta 4. Keep in mind that iOS 13.3 was in beta at the time you tested it. It was not a bug in iOS 13.2, so this is nothing to worry about anymore.

Update for iOS 13.3 release:

The bug is fixed on physical devices but is still present on emulator.

like image 89
Thomas Vos Avatar answered Oct 26 '22 03:10

Thomas Vos


I have the same problem, see this post. The problem only occurs on a physical iPad 9.7 inch. Not with in the simulator, nor on my iPhone.

like image 25
simibac Avatar answered Oct 26 '22 05:10

simibac