swift - 快速的 UITableView

标签 swift uitableview parse-platform

谁能告诉我我的代码出了什么问题,我总是在所有单元格中重复得到相同的数据

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel!.textColor = UIColor.whiteColor()
       let query = PFQuery(className:"book")
    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in
          var contentString:String = String()
        if error == nil {
            print("Success retrieved \(objects!.count) scores.")
            if let objects = objects {
                for object in objects {
               let a:String = object.objectForKey("title") as! String
                contentString = a
            }
        }
              cell.textLabel!.text = contentString
    }}
    return cell
    }

最佳答案

最好将所有数据查询到 viewDidLoad() 或任何您想要的生命周期中。

    var contentString = [String]() //<-- use an array of string instead of a string variable

 override func viewDidLoad()
   {
      super.viewDidLoad()
        let query = PFQuery(className:"book"){ query.findObjectsInBackgroundWithBlock {
    (objects: [PFObject]?, error: NSError?) -> Void in

    if error == nil {
        print("Success retrieved \(objects!.count) scores.")
        if let objects = objects {
            for object in objects 
        {
            let value = object["title"] as! String
            self.contentString.append(value) //<-- save the query value to your array 
        }
        self.tableView.reloadData() //<--- then you reload your UI
      }
   }}
}

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
   cell.textLabel!.textColor = UIColor.whiteColor()
   cell.textLabel!.text = self.contentString[indexPath.row] //<-- then assign the value into your cell 
   return cell
}

关于swift - 快速的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34568767/

相关文章:

ios - 更新缓存的 PFUser currentUser?

android - 关于Parse中LocalDataStore的问题

swift - Realm swift : always put nil values last in sort

ios - UICollectionView:从自定义单元格中更改 sizeForItemAtIndexPath

ios - tableviewcell 中的图像不会立即出现

ios - 如何使用 Swift 在 IOS 中使用自定义标题操作在 tableview 中显示 Collection View

ios - 模拟器无法获取位置

ios - UITableView 上的前卫滚动 - iOS

ios - 如何使用 Swift 3 选择器?

swift - 如何观察 bool 值流并延迟值变化?