在单击屏幕之前不会出现表格视图内容
Posted
技术标签:
【中文标题】在单击屏幕之前不会出现表格视图内容【英文标题】:Tableview contents will not appear until screen is clicked 【发布时间】:2016-01-22 20:58:20 【问题描述】:目前正在开发一个带有使用云套件填充的表格视图的 swift 应用程序。
由于某种原因,当应用程序打开时,tableview 显示为空白,但是一旦触摸屏幕,我的条目列表就会突然出现。
我在下面提供了整个 Tableview 大师班,我的想法是它与 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.
【问题讨论】:
【参考方案1】:考虑到重新加载是在完成处理程序中发生的,我假设它在后台线程上。尝试将您的 UI 更新分派回主线程。
(在此处键入没有 Xcode 的示例,因此请检查我的语法。)
例子:
dispatch_async(dispatch_get_main_queue())
self.tableView.reloadData()
【讨论】:
我不太确定我是否理解。如何控制哪些线程选择哪一段代码? 如果您只需要简单的东西,GCD 是最快的方法。这是一个参考:thatthinginswift.com/background-threads【参考方案2】:如果你尝试这些,它会起作用
DispatchQueue.main.async(execute:
self.tableView.reloadData()
)
【讨论】:
【参考方案3】:斯威夫特 5.2:
DispatchQueue.main.async
self.tableView.reloadData()
【讨论】:
以上是关于在单击屏幕之前不会出现表格视图内容的主要内容,如果未能解决你的问题,请参考以下文章