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)
    }
}
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • check out this answer. It will give you some idea: https://stackoverflow.com/a/58639134/8397245 – FRIDDAY Nov 13 '19 at 10:17

1 Answers1

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.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278