2

i have an array of model class and this model class have an array . i want to populate tableview from array . its showing me error

Here is my Model class

class DataModel {

    var name:[String]

    init(name:[String]) {
        self.name = name
    }
} 

Here is my model object :

class Data {
    static var product = [DataModel]()
}

Here is my function for append data to object :

class Function {

    static func read(compleationHandler:@escaping ()->()) {

        var model = ["nsu","iUB","UIU"]
        DispatchQueue.global(qos: .userInteractive).async {
            if Data.product.count == 0 {
                Data.product.append(DataModel(name: model))
            }
            DispatchQueue.main.async {
                compleationHandler()
            }
        }
    }
}

and here is my tableView Cell method :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell") as! myCell

        cell.textLabel?.text = Data.product[indexPath.row].name[indexPath.count]

        return cell
    }

Here is my ViewDidLoad :

myTableView.delegate = self
        myTableView.dataSource = self
        Function.read { [weak self] in
            print(Data.product.count)
            self?.myTableView.reloadData()
        }

    }
4
  • What is the error? And where is it showing? Commented Aug 23, 2018 at 6:04
  • "Thread 1: Fatal error: Index out of range" Commented Aug 23, 2018 at 6:07
  • I want to populate data from this array which belong in model "cell.textLabel?.text = Data.product[indexPath.row].name[indexPath.row]" Commented Aug 23, 2018 at 6:08
  • It's probably because you are using the wrong indexes in cellForRowAt. cell.textLabel?.text = Data.product[indexPath.section].name[indexPath.row] But i can't be sure unless i see your numberOfSections and numberOfRows delegates. Commented Aug 23, 2018 at 6:08

1 Answer 1

3

The problem probably is the way your delegates are set up. This is what i feel is what you are trying to achieve.

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell") as! myCell

    cell.textLabel?.text = Data.product[indexPath.section].name[indexPath.row]

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

2 Comments

Thank you its working but If i populate in one section how its possible ?
You have to map all the names to a one dimensional array and use that as data source. Also return 1 in numberOfSections. You can refer this to get an understanding of how to flatter multi dimensional arrays - stackoverflow.com/questions/24465281/…

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.