【问题标题】:Swift - Populating Table RowsSwift - 填充表格行
【发布时间】:2016-01-13 10:32:25
【问题描述】:

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

数组显示所有内容都在那里(它应该包含 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
    }

【问题讨论】:

  • 在您的 numberOfRowsInSection 方法中,您返回什么值?是数组的计数,还是只有 1。
  • 你实现了numberOfRowsInSection函数吗?
  • @totiG 很好地实现了捕获,但将其留空。我添加了所需的内容,但现在又出现另一个错误,显示“NSRangeException”,原因:'*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'”
  • 这可能是因为 numberOfRows 部分返回的行数比数组中的行数多。您的 numberOfRows 实现是否设置为:return friendsInfoListArray.count
  • @totiG 是的,确实是....更好的方法?

标签: ios xcode swift parse-platform


【解决方案1】:

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

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;

    }

【讨论】:

  • 我这样做了,但正如上面评论中所述,我收到以下错误:“NSRangeException”,原因:'*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 。 . 0]'"
  • 确保您的 friendsInfoListArray 应该使用所有可能的对象创建,然后重新加载表。
  • 重新加载表?你能概括一下这意味着什么吗?不熟悉抱歉
  • 编辑答案并正确实施。
  • 我正在为我的视图控制器使用 Parse 的 PFQueryTableViewController。所以有一个名为 queryForTable 的方法,我在将数据加载到表之前加载两个数组。
【解决方案2】:

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

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
    }

【讨论】:

    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多