如何在swift

时间:2018-08-11 05:01:04

标签: ios

我的值为:-

[[yes,no],[yes,no]]

我需要在tableview的cellForRowAt indexPath中显示。

我的代码:-

 var tableArray:Array<[String]> = []

从JSON获取数据:-

 if  let data = wholedata["data"] as? Array<[String:Any]>{
     print(data)
     print(response)
     for question in data {
         let options = question["options"] as! [String]
         self.tableArray.append(options)
         //  self.fetchedHome.append(options as! [home])
         self.no = options.count
     }
     print(self.tableArray)

我在表格视图中的索引行的单元格:-

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let identifier = "Cell"
        var cell: QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
        if cell == nil {
            tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
        }            
        print(reviewViewModel.tableArray)
        cell.question.text = "hai"

        } else {
            return UITableViewCell()
        }
}

如何在表格视图单元格中显示数据?

3 个答案:

答案 0 :(得分:2)

首先不要在cellForRow中注册该单元格,而在viewDidLoad中注册它。

private let identifier = "Cell" // Declare the identifier as private constant on the top level of the class.

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
    // do other stuff 
}

然后在cellForRow中从数据源数组获取索引路径项。由于这些选项是一个数组,因此必须在两个标签中显示它或将它们连接起来。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: identifier) as! QuestionListCell
    let options = tableArray[indexPath.row]

    cell.question.text = options.joined(separator: ", ")
    return cell
}

我猜您想显示的数据比options多。但是目前,您仅使用options

填充数据源数组

再次按照我对您以前的问题之一的回答的建议,我强烈建议将JSON解码为自定义结构。它使生活变得更加轻松。

答案 1 :(得分:0)

这是一个非常简单的问题,兄弟。

如果您不介意使用本节,请尝试一下〜!!!

var tableArray:Array<[String]> = []

func viewDidLoad() {
    let table = UITableView()
    table.register(YourNib, forCellReuseIdentifier: YourIdentifier)
}

func numberOfSections(in tableView: UITableView) -> Int {
    return tableArray.count
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let tempArray = tableArra[indexPath.section] as? [String],
        let cell = tableView.dequeueReusableCell(withIdentifier: YourIdentifier) as? QuestionListCell {
        // just Initialize your cell with data in datasource
        cell.question.text = tempArray[indexPath.row]
        return cell
    } else {
        return UITableViewCell()
    }
}

答案 2 :(得分:0)

您可以将array of array转换为array,然后在表中显示,并且tableViewController将仅包含array数据。

您可以使用 flatmap / compactMap array of array转换为array

let a = ["yes","no"]
let b = [a, a]      // [["yes","no"], ["yes","no"]]
b.flatMap({$0})     // ["yes","no", "yes","no"]
相关问题