Ios 如何在tableviewcell中显示JSON中的数据

Ios 如何在tableviewcell中显示JSON中的数据,ios,Ios,我需要在tableview中列出数据 {"data":[{"id":33,"question":"sqws","options":["option ,A","option ,A"],"button_type":"2","option_count":"2"}]} 这是我的api模式 我的代码如下:- Model:- class Model: NSObject { var question:String? var options:[String]?

我需要在tableview中列出数据

{"data":[{"id":33,"question":"sqws","options":["option ,A","option ,A"],"button_type":"2","option_count":"2"}]}
这是我的api模式

我的代码如下:-

Model:-


class Model: NSObject {

        var question:String?
        var options:[String]?

        init?(dictionary :JSONDictionary) {

            guard
                let question = dictionary["question"] as? String

                else {
                    return

            }

            if let options = dictionary["options"] as? [String]{
                print(options)

                self.options = options

            }


            self.question = question
             }

    }
数据源模型:-

class DataSourceModel: NSObject {

    var dataListArray:Array<Model>? = []

    init(array :Array<[String:Any]>?) {
        super.init()
        var newArray:Array<[String:Any]> = []
        if array == nil{

            // newArray = self.getJsonDataStored22()
        }
        else{
            newArray = array!

        }

        var datalist:Array<Model> = []
        for dict in newArray{

            let model = Model(dictionary: dict)

            datalist.append(model!)
        }
        self.dataListArray = datalist
    }

}
问题标题:-

 class questionheader: UITableViewCell {


        @IBOutlet weak var lblName: UILabel!
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
        func setReviewData(reviews:QuestionListModel)

        {
            self.lblName.text = reviews.question
            print(self.lblName.text)

        }


        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }

    }
问题列表单元格:-

class QuestionListCell: UITableViewCell {

    @IBOutlet weak var question: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    func setReviewData(reviews:QuestionListModel)

    {

        print(reviews.options)
        print(reviews.options?[0])

        self.question.text = reviews.options?[0]
        print(self.question.text)

    }


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}
所以,在这里我有一些问题,但根据我的api,我需要列出包含在key选项中的数据

{"data":[{"id":33,"question":"sqws","options":["option ,A","option ,B"],"button_type":"2","option_count":"2"}]}
根据这一点,我得到了第节中的问题,我需要在tableviewcell中显示选项,这意味着选项A和选项B应该显示在tableviewcell中。怎么做

就像这样

第一件事是:

您应该在此方法中返回选项计数

 func numberOfRowsInSection(section:Int) -> Int {

    let objectAtSection: [String: String] = datasourceModel.dataListArray?[section]
    return Int(objectAtSection["option_count"])!
}
其次,要在单元格中显示选项,请更新此方法

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

    if cell == nil {
        tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
        cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
    }

   let objectAtSection: [String: String] = datasourceModel.dataListArray?[section]
   let optionsAtRow: [String] = objectAtSection["options"]
   cell.question.text = optionsAtRow[indexPath.row]
   return cell
}

尝试并分享结果

如何解决问题通过执行代码添加您看到的结果的屏幕截图。@BhavinKansagara我已经添加了屏幕截图。请检查。最好转发从
NSObject继承的github@Nahlabdulnawalwhy类上的项目链接
?这不是客观的C。为什么在许多情况下会有这么多的选项被强制展开?永远不要自己调用包含
will
did
should
的委托方法。我需要以mvvm的方式执行。因此,如何以这种方式实现我仍然没有得到输出用编辑后看到的结果更新您的问题
 func numberOfRowsInSection(section:Int) -> Int {

    let objectAtSection: [String: String] = datasourceModel.dataListArray?[section]
    return Int(objectAtSection["option_count"])!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifier = "Cell"
    var cell: NH_QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell

    if cell == nil {
        tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
        cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
    }

   let objectAtSection: [String: String] = datasourceModel.dataListArray?[section]
   let optionsAtRow: [String] = objectAtSection["options"]
   cell.question.text = optionsAtRow[indexPath.row]
   return cell
}