OGeek|极客世界-中国程序员成长平台

标题: ios - 数组填充并显示在函数中但没有显示在tableview swift中 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:55
标题: ios - 数组填充并显示在函数中但没有显示在tableview swift中

我遇到了一个数组,它在一个单独的函数中正确填充,问题是当我尝试在我的表格 View 中填充我的单元格的元素时,我只能找到最后一个元素但是当我想要在填充单元格时显示该数组中的元素数,它显示正确的元素数,有人可以帮忙吗? 这是我检索和填充数组的函数:

  func downloadUserDetails(completed: @escaping DownloadComplete){

    let Ful_Url = "http://192.168.1.4:8888/phps/select.php"

    Alamofire.request(Ful_Url).responseJSON(completionHandler:  { (response) in

        if let userDect = response.result.value as? [Dictionary<String,AnyObject>]{

            for ex in 0...userDect.count-1
            {
                if let firstnames = userDect[ex]["firstname"] as? String{
                   self.users?.firstname = firstnames}

                if let emails = userDect[ex]["email"] as? String{
                    self.users?.email = emails}

                if let lastnames = userDect[ex]["lastname"] as? String{
                    self.users?.lastname = lastnames}

                 print("---------------------------------")


                self.items.append(self.users!)

               // self.items.insert(self.users!, at: self.i)

                print(self.items[ex].email)
                print(self.items.count)
                }
        }
        completed()
        self.tableview.reloadData()
    })

}

这就是我尝试填充单元格标签的方式:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("####################")
    print("nombre items")
    print(self.items.count)
    return items.count


}

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

    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier:"myCell" , for:indexPath)

    let email:UILabel = cell.viewWithTag(11) as! UILabel
    let firstname:UILabel = cell.viewWithTag(12) as! UILabel
    let lastname:UILabel = cell.viewWithTag(13) as! UILabel

        print("=========================email=========================")
        print(items[indexPath.row].email)

        email.text = items[indexPath.row].email
        firstname.text = items[indexPath.row].firstname
        lastname.text = items[indexPath.row].lastname


    return cell

}



Best Answer-推荐答案


我认为插入方法有问题:

self.item.insert(self.users!, at:self,i);

你可以试试:

self.item.insert(self.users!, at:ex);

关于ios - 数组填充并显示在函数中但没有显示在tableview swift中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44679915/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4