0

I have an object that I map into an empty array of string. So I can populate the string data, but the weird thing is, when I try to paste the data inside an array of objects it shows the data. In the example below I show you what I mean.

// My view Controller
    var filterClasses = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = #colorLiteral(red: 0.8470588235, green: 0.8470588235, blue: 0.8470588235, alpha: 0.66)

        populateClasses()

        // The array not show up in my tableViewCell
        chooseClass.dropView.options = filterClasses

        // The array show up in my tableViewCell
        // This data is from filter class
        chooseClass.dropView.options = ["8-1", "8-10", "8-2", "8-3", "8-4", "8-5", "8-6", "8-7"]
    }

    private func populateClasses() {
        AddScheduleServices.shared.getListClass { [weak self] result in
            switch result {
            case .success(let classes):
                guard let self = self else { return }
                self.filterClasses = classes.compactMap { $0.name }
                print(self.filterClasses)
            case .failure(let error):
                print(error)
            }
        }
    }

    // This is a custom view from "chooseClass.dropView.options"
    class DropDownView: UIView, UITableViewDelegate, UITableViewDataSource {

    let tableView           = UITableView()
    var options             = [String]()

    var completion: ((String) -> Void)?
    var isHideSchedule      = false

    override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func configure() {
        translatesAutoresizingMaskIntoConstraints = false
        addSubview(tableView)
        tableView.backgroundColor = #colorLiteral(red: 0.9764705882, green: 0.9764705882, blue: 0.9764705882, alpha: 1)
        tableView.separatorStyle = .none
        tableView.delegate      = self
        tableView.dataSource    = self

        tableView.anchor(top: topAnchor, trailing: trailingAnchor, bottom: bottomAnchor, leading: leadingAnchor, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 0, height: 0)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return options.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        cell.textLabel?.text = options[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        completion?(options[indexPath.row])

        tableView.deselectRow(at: indexPath, animated: true)
    }
}

I don't what went wrong with my array chooseClass.dropView.options. Can you tell me why my array is not displayed in my tableview?

1
  • seems option array always empty. because no data append to it anywhere in the code Commented Mar 26, 2020 at 16:20

1 Answer 1

1

The operation is async I suppose AddScheduleServices.shared.getListClass, so you have to reload the table after fetching data.

1. update options on success:

private func populateClasses() {
       //...
        case .success(let classes):
            guard let self = self else { return }
            self.filterClasses = classes.compactMap { $0.name }
            self.chooseClass.dropView.options = self.filterClasses // <-- here
        //...
        }
}

2. reload table view when set new options:

class DropDownView: UIView, UITableViewDelegate, UITableViewDataSource {

    //...
    var options  = [String]() {
        didSet {
            self.tableView.reloadData()
        }
    }
    //...
}
Sign up to request clarification or add additional context in comments.

1 Comment

hey thank you @AndrewBogaevskyi it might be not work because I place the chooseClass.dropView.options in viewDidLoad. and not reloading data in tableView like you did.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.