I'm trying to show data in UITablleView after getting it from an HTTP request. I heard that the best and professional way to do it is configure the cell and do the call request in model class then send it to the view controller.

Here is what I tried to do, but I think it's not going well. I know this is a wrong way but I wanted to show you what I got based on someone.

UITable View Cell

@IBOutlet weak var status : UILabel!
@IBOutlet weak var date : UILabel!
@IBOutlet weak var model : UILabel!

 func ConfigCell (car : Car){    
  let urlstr = "http://localhost:8000/api/newuser/check"
  let url = URL(string: urlstr)
  guard let token = UserDataSingleton.sharedDataContainer.token else { return    }
  let headers = ["Authorization":"Bearer \(token)"]
  var statusCode: Int = 0
request(url! ,  method: .get,  encoding: JSONEncoding.prettyPrinted ,      headers: headers )
    .responseJSON { response in
        if let value: AnyObject = response.result.value as AnyObject? {
            //Handle the results as JSON
            let json = JSON(value)                  
            for (key, subJson) in json["allcar"] {
                if let status = subJson["status"].string {
                    self.status.text = status
                    if let date = subJson["created_at"].string {
                          self.date.text = date

                        if let model = subJson["model"].string {
                            self.model.text = model
                            let Status = [
                                Car(model: model, status: status, date: date)]

Car model

  class Car {

private var _mode : String?
private var _status : String?
private var _date : String?

var model : String{
return _model!
    var status : String {
    return _status!
      var date : String {
       return _date!

init(model : String , status :String,date : String) {
        self._status = status
        self._model = model
        self._date = date


class testViewController: UIViewController, UITableViewDelegate , UITableViewDataSource {

    let data = [Car]()

   @IBOutlet weak var tableview: UITableView!

      override func viewDidLoad() {

   tableview.delegate = self
   tableview.dataSource = self

       func numberOfSections(in tableView: UITableView) -> Int {
          return 1
         func tableView(_ tableView: UITableView, numberOfRowsInSection section:  Int) -> Int {
       return data.count
         func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     let cell = tableView.dequeueReusableCell(withIdentifier: "CarCell",for: indexPath) as! CarC

      let entry = data[indexPath.row]
       cell.date.text = entry.date
      cell.model.text = entry.model
      cell.status.text = entry.status
      return cell

There are lot of ways to organize an architecture. I will explain how do I understand some of them. Read about MVC and MVVC patterns.

  1. First of all, it would be better do divide all code related to requests and code responsible for UI.
  2. Creating a model class is a good way. Model can only keep and parse data. It shouldn't contain any business logic.
  3. You can create one more type class. Such as presenter or viewModel. This class would manage all contains in this module. Contains the cars array in your case. And here you can ask the ServerInteractor class to run the requests.
  4. All code related to requests would be better to keep in ServerInteractor (or LocalInteractor).
  5. It is a good practice to write methods smaller if it is possible. For example in your cellForRowAt method you can divide all code such as "cell.date.text = entry.date" to the separate method

I wish it would be useful )

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.