elprl / TDLikertScaleSelectorView

UI Control for displaying a Likert Scale question
MIT License
2 stars 1 forks source link

How to programatically set likert value in a large table #3

Open JoaquinMG opened 5 years ago

JoaquinMG commented 5 years ago

Hello. The code works perfectly, but in a large table of items, when scrolling, cells lose their value. I guess I know how to keep the results (array) but how do I change the state of a question (answer button) programatically? Thanks

elprl commented 5 years ago

I haven't added any datasource logic. You'll have to create a collection to store the values. This logic should go with your viewModel logic for the tableview I imagine.

JoaquinMG commented 5 years ago

When you say collection, do you mean array (I’m still learning). About datasource, should I declare it on example.swift? Thanks

Quin

El 22 may 2019, a las 14:53, elprl notifications@github.com escribió:

I haven't added any datasource logic. You'll have to create a collection to store the values. This logic should go with your viewModel logic for the tableview I imagine.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

elprl commented 5 years ago

Look at ExampleTableView.swift:

open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var styleConfig = MyBuildConfig()

        switch indexPath.row {
        case 0:
            let cell = UITableViewCell(style: .default, reuseIdentifier: "questionCellId")
            cell.textLabel?.text = "Q1. President Trump is doing good work."
            return cell
        case 1:
            let cell = UITableViewCell(style: .default, reuseIdentifier: "answerCellId")
            cell.selectionStyle = .none
            let likertView = TDLikertScaleSelectorView(withConfig: styleConfig)

then access the UIStackView in the TDLikertScaleSelectorView which contains the Buttons, then set isSelected.

JoaquinMG commented 5 years ago

Thank you very much, but it's too difficult for me. I' changed a few things in cell (question and answer cells are now one):

`open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var styleConfig = MyBuildConfig()

    switch indexPath.row {
    case indexPath.row:
        let cell = UITableViewCell(style: .default, reuseIdentifier: "questionCellId")
        cell.textLabel?.text = "\(indexPath.row + 1). \(preguntas[indexPath.row])\n\n\n\n"
        cell.textLabel?.adjustsFontSizeToFitWidth = true
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.sizeToFit()
        styleConfig.buttonRadius = 15
        styleConfig.font = UIFont.boldSystemFont(ofSize: 14)

        cell.selectionStyle = .none
        let likertView = TDLikertScaleSelectorView(withConfig: styleConfig)
        likertView.delegate = self
        likertView.tag = indexPath.row
        likertView.clipsToBounds = true
        cell.contentView.addSubviewForAutoLayout(likertView)
        likertView.pin(to: cell.contentView, withPadding: 10)

        return cell

    default:
        return UITableViewCell()
    }
}`

When likert is touched, I change the value of a boolean array to true. I guess I have to use a if statement to check if it has been selected, and then create a function (?) for each selected option. Thanks again.