Swift - 填充表格行

时间:2016-01-13 10:32:25

标签: ios xcode swift parse-platform

我正在填充一个表,其中包含从后端获取的名称列表(作为用户名,然后作为朋友的名字)。

数组显示所有内容都存在(它应该包含2条记录)但由于某种原因,我正在做一些愚蠢的事情,导致在表视图中只显示一条记录(单行)。

我个人认为,由于实现了cellForRowAtIndexPath方法,这是一个错误。这是代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        let cellIdentifier = "cell"

        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FriendsCell
        if cell == nil {
            cell = FriendsCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
        }

        // Configure the cell to show name of friend
        print("Index Row: \(indexPath.row)")
        cell!.name.text = self.friendsInfoListArray[indexPath.row]["name"] as? String


        return cell
    }

有人能发现哪里出错了?如上所述,阵列中有两条记录,但只显示一条记录。

感谢

更新

reloadTable的代码:

override func queryForTable() -> PFQuery {

        self.loadFriendsUsernames({ (result)->Void in
            if(result.count > 0)
            {
                self.loadFriendsInfo({ (result)->Void in
                      if(result.count > 0)
                      {
                        print(self.friendsInfoListArray)
                        self.tableView.reloadData()

                        print("Success Loading Friends Info Array")
                      }else{
                       print("Error Loading Friends Info Array")
                      }
                })
            }else{
                print("Error Loading Friends Usernames Array")
            }

        })


        let query = PFQuery(className: self.parseClassName!)
        let currentUser = PFUser.currentUser()
        query.whereKey("appUsername", equalTo: currentUser!["appUsername"])
        // If no objects are loaded in memory, we look to the cache first to fill the table
        // and then subsequently do a query against the network.
        if self.objects!.count == 0 {
            query.cachePolicy = .CacheThenNetwork
        }

        return query
    }

2 个答案:

答案 0 :(得分:1)

假设您在viewDiDLoad:method中获取好友列表。

override func queryForTable() -> PFQuery {

    self.loadFriendsUsernames({ (result)->Void in
        if(result.count > 0)
        {
            self.loadFriendsInfo({ (result)->Void in
                  if(result.count > 0)
                  {
                    print(self.friendsInfoListArray)
                    self.friendsInfoListArray = result
                    self.tableView.reloadData()

                    print("Success Loading Friends Info Array")
                  }else{
                   print("Error Loading Friends Info Array")
                  }
            })
        }else{
            print("Error Loading Friends Usernames Array")
        }

    })


    let query = PFQuery(className: self.parseClassName!)
    let currentUser = PFUser.currentUser()
    query.whereKey("appUsername", equalTo: currentUser!["appUsername"])
    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    if self.objects!.count == 0 {
        query.cachePolicy = .CacheThenNetwork
    }

    return query
}


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

         return self.friendsInfoListArray.count;

    }

答案 1 :(得分:0)

经过长时间的努力,我找到了适合我的解决方案:

override func queryForTable() -> PFQuery {
          let query = PFQuery(className: self.parseClassName!)
        self.loadFriendsUsernames({ (result)->Void in
            if(result.count > 0)
            {
                self.loadFriendsInfo({ (result)->Void in
                      if(result.count > 0)
                      {
                        print(self.friendsInfoListArray)
                        self.tableView.reloadData()
                         let currentUser = PFUser.currentUser()
        query.whereKey("appUsername", equalTo: currentUser!["appUsername"])
        // If no objects are loaded in memory, we look to the cache first to fill the table
        // and then subsequently do a query against the network.
        if self.objects!.count == 0 {
            query.cachePolicy = .CacheThenNetwork
        }

                        print("Success Loading Friends Info Array")
                      }else{
                       print("Error Loading Friends Info Array")
                      }
                })
            }else{
                print("Error Loading Friends Usernames Array")
            }

        })




        return query
    }