菜鸟教程小白 发表于 2022-12-11 19:55:02

ios - 数组填充并显示在函数中但没有显示在tableview swift中


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

<pre><code>func downloadUserDetails(completed: @escaping DownloadComplete){

    let Ful_Url = &#34;http://192.168.1.4:8888/phps/select.php&#34;

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

      if let userDect = response.result.value as? {

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

                if let emails = userDect[&#34;email&#34;] as? String{
                  self.users?.email = emails}

                if let lastnames = userDect[&#34;lastname&#34;] as? String{
                  self.users?.lastname = lastnames}

               print(&#34;---------------------------------&#34;)


                self.items.append(self.users!)

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

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

}
</code></pre>

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

<pre><code>func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    print(&#34;####################&#34;)
    print(&#34;nombre items&#34;)
    print(self.items.count)
    return items.count


}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier:&#34;myCell&#34; , 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(&#34;=========================email=========================&#34;)
      print(items.email)

      email.text = items.email
      firstname.text = items.firstname
      lastname.text = items.lastname


    return cell

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为插入方法有问题:</p>

<pre><code>self.item.insert(self.users!, at:self,i);
</code></pre>

<p>你可以试试:</p>

<pre><code>self.item.insert(self.users!, at:ex);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 数组填充并显示在函数中但没有显示在tableview swift中,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44679915/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44679915/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 数组填充并显示在函数中但没有显示在tableview swift中