2016-01-22 78 views
0

開發一個快速的應用程序,此時使用雲工具包填充的表格視圖。Tableview內容將不會出現,直到點擊屏幕

由於某些原因,當應用程序打開時,tableview顯示爲空白,但一旦屏幕被觸摸,我的列表中的條目突然出現。

我在下面提供了我的整個Tableview Master Class,我的想法是它與viewDidLoad()函數有關,但無論我嘗試什麼,我似乎都無法解決這個問題。

import UIKit 
import CloudKit 
import MobileCoreServices 

class MasterViewController: UITableViewController { 

    //// DB setup (outside DidLoad) 
    let container = CKContainer.defaultContainer() 
    var Database: CKDatabase? 
    var currentRecord: CKRecord? 

    var detailViewController: DetailViewController? = nil 

    ///Array to save dbrecords 
    var objects = [CKRecord]() 

    /// Initialise object of ClipManager class 
    let MyClipManager = ClipManager() 






    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Database loading on runtime 
     Database = container.privateCloudDatabase 

     ///Build Query 
     let query = CKQuery(recordType: "CloudNote", predicate: NSPredicate(format: "TRUEPREDICATE")) 

     ///Perform query on DB 
     Database!.performQuery(query, inZoneWithID: nil) { (records, error) -> Void in 
      if (error != nil) { 
       NSLog("Error performing query. \(error.debugDescription)") 
       return 
      } 

      self.objects = records! 
      self.tableView.reloadData() 
     } 


     // Do any additional setup after loading the view, typically from a nib. 
     self.navigationItem.leftBarButtonItem = self.editButtonItem() 

     let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:") 
     self.navigationItem.rightBarButtonItem = addButton 
     if let split = self.splitViewController { 
      let controllers = split.viewControllers 
      self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController 
     } 
    } 

    override func viewWillAppear(animated: Bool) { 
     self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed 
     super.viewWillAppear(animated) 
    } 




    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 





    // Tableview stuff --- Done 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 
      /////// Get number of rows 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return objects.count 
    } 
      //// FIll the table 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 

     let object = objects[indexPath.row] 
     cell.textLabel!.text = object.objectForKey("Notes") as? String 
     return cell 
    } 

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     // Return false if you do not want the specified item to be editable. 
     return true 
    } 
    //// Deleting the table 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == .Delete { 

      //DB call working 
      MyClipManager.DeleteMethod(Database!, MyRecord:objects[indexPath.row]) 

      objects.removeAtIndex(indexPath.row) 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 


     } else if editingStyle == .Insert { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
     } 
    } 


} 

回答

3

考慮到重裝是在完成處理程序中發生的,我假設它在後臺線程上。嘗試將您的UI更新分發回主線程。

(不打字這裏的Xcode的樣本,所以檢查我的語法。)

例子:

dispatch_async(dispatch_get_main_queue()) { 
    self.tableView.reloadData() 
} 
+0

我也不太知道我的理解。我怎樣才能控制哪些線程選擇哪一個代碼? – Malorrr

+0

如果你只需要簡單的東西,GCD是最快捷的方式。這裏有一個參考:https://thatthinginswift.com/background-threads/ –

+0

工作完美菲利普,謝謝! – Malorrr