Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behaviour with empty initial value #26

Open
brandFromNSK opened this issue Feb 22, 2020 · 6 comments
Open

Unexpected behaviour with empty initial value #26

brandFromNSK opened this issue Feb 22, 2020 · 6 comments

Comments

@brandFromNSK
Copy link

Hello! Here is an example where I expect the grid appears after button tap, but I got empty grid

struct ContentView: View {
	
	@ObservedObject var viewModel = ContentViewModel()
	
	var body: some View {
		VStack {
			Button("Get all values") {
				self.viewModel.tap()
			}
			Text(String(self.viewModel.models.count))
			QGrid(self.viewModel.models, columns: 3) {
				GridCell(value: $0)
			}
		}
	}
}

class ContentViewModel: ObservableObject {
	
	@Published var models: [String] = [ ]
	
	func tap() {
		self.models = ["1", "2", "3"]
	}
}

extension String: Identifiable {
	public var id: String {
		return self
	}
}

struct GridCell: View {
	var value: String
	
	var body: some View {
		Text(self.value)
	}
}

However it works correctly if models value has at least one initial element. Can you explain what should I do in this case? Or is it a bug?

@stefangerard
Copy link

stefangerard commented Feb 24, 2020

I had the same issue. It's because of the ScrollView inside the Grid. ScrollView can not be initialized with an empty array.
In my opinion it is a bug and Apple should fix it.

A workaround is to check if the data are empty or not. See following link.
https://forums.raywenderlich.com/t/swiftui-dont-update-scrollview-content-via-api/88875/4

For now I am using a copy of QGrid in my Project and I modified it like this (see 3rd line):

    public var body: some View {
        GeometryReader { geometry in
            if !self.data.isEmpty {
                ScrollView(showsIndicators: false) {
                    VStack(spacing: self.vSpacing) {
                        ForEach((0..<self.rows).map { JGridIndex(id: $0) }) { row in
                            self.rowAtIndex(row.id * self.cols,
                                            geometry: geometry)
                        }
                        // Handle last row
                        if self.data.count % self.cols > 0 {
                            self.rowAtIndex(self.cols * self.rows,
                                            geometry: geometry,
                                            isLastRow: true)
                        }
                    }
                }
                .padding(.horizontal, self.hPadding)
                .padding(.vertical, self.vPadding)
            }
        }
    }

@brandFromNSK
Copy link
Author

Anyway, I think QGrid should have any workaround

@Svantulden
Copy link

Somehow, adding .id(UUID() to the QGrid view worked for me 🤔. Don't know why, because the empty scrollview would be the logical explanation for my case too, as I also load the data asynchronously, so the array is empty as first.

@mightyquinn408
Copy link

@Svantulden Where did you add .id(UUID() to the QGrid?

@Svantulden
Copy link

If I take the example from the first post here:

struct ContentView: View {
	
	@ObservedObject var viewModel = ContentViewModel()
	
	var body: some View {
		VStack {
			Button("Get all values") {
				self.viewModel.tap()
			}
			Text(String(self.viewModel.models.count))
			QGrid(self.viewModel.models, columns: 3) {
				GridCell(value: $0)
			}.id(UUID())
		}
	}
}

This seemed to work for me

@chabose
Copy link
Contributor

chabose commented Aug 1, 2020

WA mentioned above leads unpredicted behavior on tap gestures for me
Also, I found It can be avoided by checking if data is empty on the library.
I will send a pull request shortly.

karolkulesza added a commit that referenced this issue Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants