0

When I use init(check the sourcecode: from Line 10), bellow error is coming.

Return from initializer without initializing all stored properties

If I use UITableView.appearance().backgroundColor = .clear in didFinishLaunchingWithOptions it works fine. but that should be the case know. i need to change the color of list in one contoller alone.

Here is my code:

import UIKit
import SwiftUI
struct PortalSetupView: View {
@State var title = ""
@Binding var isNavigationBarHidden: Bool
@State var name: String = ""
let datasource: [String] = ["Hi", "Hello"]

init() {
    UITableView.appearance().backgroundColor = .clear // tableview background
    UITableViewCell.appearance().backgroundColor = .clear // cell background
}
var body: some View {
    VStack(alignment:.leading){
        Text("Hey")
            .font(.system(size: 17))
            .padding(.init(top: 10, leading: 0, bottom: 30, trailing: 0))
        TextField("Hey there", text: $name)
        Divider().padding(.init(top: 0, leading: 0, bottom: 20, trailing: 0))
        List{
            Section(header: Text("My header").font(.system(size: 15))) {
                ForEach(datasource, id: \.self) { item in
                    RegionView(region: item)
                }
            }
        }.background(Color.yellow)
            .listStyle(GroupedListStyle())

        Button(action: {
        }){
            Text("My Button").foregroundColor(.white)
                .frame(minWidth: 0, maxWidth: .infinity)
        }.padding()
            .background(Color("LightRed"))
            .cornerRadius(10)
    }
        .navigationBarTitle("My Navigation")
        .navigationBarBackButtonHidden(true)
        .onAppear {
            self.isNavigationBarHidden = false
        }.padding([.leading, .trailing], 18)
    }
}
struct RegionView: View {
    var region: String
    var body: some View {
        Text(region)
    }
}
1

1 Answer 1

0

The issue has nothing to do with the List. Xcode is trying to tell you that you don't initialize all stored properties.

In the code you provided, you forgot to set an initial value for isNavigationBarHidden variable

Swift have a strict rule that forces you to set an initial value for every variable you define before trying to get it. So when you are implementing an init() method, you must initialize all stored properties that have no initial value before returning from the function.

Since you are using @Binding and you are probably setting it from previous view (due to comments), you may move the init() code outside of the class somewhere else like onAppear

You may want to do it in onAppear and undo it in onDisappear to make it look like it's only for for that controller.

Sign up to request clarification or add additional context in comments.

6 Comments

That's @Binding variable and we shouldn't set any initial value as its coming from the previous screen.
You are using custom initializer, so you should should.
Ok understood. Please tell me the way to change the background color of list.
My approach is via UITableview.appearance. But i need with in the scene. Not whole app.
Look at the last line.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.