简体   繁体   中英

How to show alamofire post request json value in table view using Swift

am try to convert json data to table view for swift 5 and also alamofire 5.2 version I got my response data from server itz also covert json but the problem is I can't show my response data in table view

class ViewController:  UIViewController, UITableViewDataSource {
    struct User {
           var buzzyuser_id:Int?
           var buzzyuser_image:String?
            var buzzyuser_username:String?

           init(dic:[String: Any]) {
               self.buzzyuser_id = dic["buzzyuser_id"] as? Int
               self.buzzyuser_image = dic["buzzyuser_image"] as? String
               self.buzzyuser_username = dic["buzzyuser_username"] as? String
           }
    }


    @IBOutlet weak var TableView: UITableView!

    private var users = [User] ()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.


        TableView.dataSource = self

        apiload()
    }
    func apiload() {
        let parameters: [String: Any] = ["userid": "1","start":"0"]
                  let url = "https://example.com/sample.php"

             AF.request(url, method: .post, parameters: parameters).responseJSON { response in
                 switch response.result{
                         case .success:
                            //success, do anything


                            if let json = response.value as? [String: Any] {
                                    for item in json {
                                                           // construct your model objects here
                                        self.users.append(User(dic:json))
                                                         }

                            DispatchQueue.main.async {
                              self.TableView.reloadData()
                            }
                            }

                            break
                 case .failure:

                                   return
                               }
             }
    }


    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customcell")
        let text = users[indexPath.row]
        return cell!
    }
}

debug value showed but list viewed only empty rows I can't find the error.

your problem is here

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 
UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customcell")
    let text = users[indexPath.row]
    return cell!
}

you are assigning the user value to the param "text" and then you are not assigning any value to the table view cell.

If you are trying... for example to show on the table the user "buzzyuser_username" then after

let text = user[indexPath.row] 

you should add

cell?.textLabel?.text = text.buzzyuser_username

Also make sure you are registering the identifier "customcell"... if you wish to load the cell from a xib file you can do it like this (note that with a xib file you can also create a custom subclass of UITableViewCell and make a tableview row with the user image id and username (that is what I think you are trying to archive)

self.table?.register(UINib.init(nibName: "YOUR_NIB_NAME", bundle: nil), forCellReuseIdentifier: "customcell")

And after registering the reuse identifier you can then use it on your table view and if you also have added a custom class to your identifier you can force it on your tableview datasource implementation

let cell = tableView.dequeueReusableCell(withIdentifier: "customcell") as! YOUR_CUSTOM_SUBCLASS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM