2016-01-02 38 views
0

谁能告诉我什么是错我的代码,我总是得到所有细胞的循环内的UITableView在迅速

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 
    } 
+0

这类似于使用的tableview小区ID应独一无二 –

回答

0

这将更好地查询所有的数据到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 
} 
0

内容应该是这样的重复相同的数据。

let a:String = object.objectForKey("title") as! String 
contentString += a 

我注意到你的块总是调用相同的查询。你能告诉我什么是不同的吗

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 
}} 
0

cellForRowAtIndex查询是麻烦。想象一下,每次看到一个单元格时,代码都要求从服务器读取每本书。 (例如,viewWillAppear)。查询完成后,使用结果初始化数组属性(例如self.bookObjects = objects)。

然后在cellForRowAtIndex ...

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
cell.textLabel!.textColor = UIColor.whiteColor() 

// this is the important idea... 
let object = self.bookObjects[indexPath.row] 

let a:String = object.objectForKey("title") as! String 
cell.textLabel!.text = a 
return cell