Sudhir
Sudhir

Reputation: 61

Displaying data in table view

I have created two label and i want to display the question in one label and answer with option in other label . I have to display options with selectable button just like multiple choice question type in single line to select , only one answer should be selected at a time

Suggestions will be appreciated

I hereby attached my code along with the screenshot of my output screen of emulator.

Code -

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var titleLabel: UILabel!


    @IBOutlet weak var topicLabel: UILabel!


 var dictionary1:[Int:String] = [0:"Whether you have experienced Pricking-pain, Desquamation,itching or dry skin sensation during seasonal alternate.",
1:"Whether your skin apt to flush( Redness) in hot humid environment ",
 2:"Whether your skin has multiple disernible dilated capillaries.",
 3:"whether you have once been diagnosed atopic dermatitis or seborrheic dermatitis."]


var dictionary2:[Int:Array<String>] = [0:["Never","Seldom","Usually","Always"],
1:["Never","Seldom","Usually","Always"],
2:["Never","Seldom","Usually","Always"],
3:["Yes", "No"]]

    override func viewDidLoad() {
        super.viewDidLoad()
        titleLabel.text = "Fill Skin Type Survey Form "
        titleLabel.textColor = UIColor.black
        topicLabel.text = "Are You with sensitive skin type ?"
        topicLabel.font = UIFont.boldSystemFont(ofSize: 18)



    }



    //TableView



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dictionary1.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        //cell.questionLabel.text = NSObject.dictionaryWithValues(forKeys: [indexPath.row])

       cell.questionLabel.text = dictionary1[indexPath.row]

        cell.questionLabel.textColor = UIColor.black
        //cell.optionsLabel.text = dictionary2[indexPath.row]

        let arr = dictionary2[indexPath.row]

       var strr = String()
        for str in arr! {
           strr = strr + str
       }
        cell.optionsLabel.text = strr

        cell.optionsLabel.textColor = UIColor.black


            return cell
    }

Upvotes: 0

Views: 160

Answers (1)

Kuldeep
Kuldeep

Reputation: 4552

You have to set UILabel numberOfLines and lineBreakMode property.

Don't forget to add heightForRowAt method.

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell

    // FOR FIRST DICTIONARY
    cell.questionLabel?.numberOfLines = 0
    cell.questionLabel?.lineBreakMode = .byWordWrapping

    cell.questionLabel?.text = dictionary1[indexPath.row]

    // FOR SECOND DICTIONARY
    cell.optionsLabel?.numberOfLines = 0
    cell.optionsLabel?.lineBreakMode = .byWordWrapping

    cell.optionsLabel?.text = dictionary2[indexPath.row]?.joined(separator: "\n")

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 350.0
}

Now it looks like this. enter image description here

Upvotes: 2

Related Questions