簡體   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