繁体   English   中英

在表格视图中显示数据

[英]Displaying data in table view

我已经创建了两个标签,我想在一个标签中显示问题,并在其他标签中选择答案。 我必须显示带有可选按钮的选项,就像单行中的多项选择题类型一样,一次只能选择一个答案

建议将不胜感激

我在此附上我的代码以及模拟器输出屏幕的屏幕截图。

代码-

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
    }

您必须设置UILabel numberOfLineslineBreakMode属性。

不要忘记添加heightForRowAt方法。

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
}

现在看起来像这样。 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM