0

I have the values as:-

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

i need to display in cellForRowAt indexPath in tableview .

my code:-

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

fetching the data from 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)

my cell for row at index in tableview :-

 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()
        }
}

How to display the data in the tableview cell?

1
  • how to solve it? Commented Aug 11, 2018 at 5:07

3 Answers 3

2

First of all don't register the cell in cellForRow, register it in 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 
}

Then in cellForRow get the item for index path from the data source array. As the options are an array you have to display it in two labels or join the items.

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
}

I guess you want to display more data than the options. But at the moment you are populating the data source array only with the options

Once again as suggested in my answer to one of your previous questions I highly recommend to decode the JSON into custom structs. It makes life so much easier.

Sign up to request clarification or add additional context in comments.

Comments

0

You can convert array of array into array before displaying in table and your tableViewController will only have the array of data.

You can use flatmap/compactMap to convert array of array to array:

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

Comments

0

This is very easy question bro.

If you don't mind using section then try this~!!!

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()
    }
}

4 Comments

here what should the YourCustomCell(withData:
you have to create your custom cell subclass of UITableViewCell.
please check i have updated my cellforrow at index.In that how to do?
I changed my answer please check it out . I don't think my code would perfectly match your goal but i hope it will help you~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.