2016-01-02 67 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