.reloadData()和.setNeedsDisplay()被忽略

时间:2016-03-30 15:42:13

标签: ios swift uitableview

我从工具栏中创建了一个分页工具:

func nextPage(sender: UIBarButtonItem) {
    let currentChapter = page.valueForKey("chapter") as! Int
    let appDelegate    = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let fetchRequest   = NSFetchRequest(entityName: "Page")
    fetchRequest.predicate = NSPredicate(format: "(chapter = %d)", currentChapter + 1)
    do {
        let result = try managedContext.executeFetchRequest(fetchRequest)



        // It is here, I can clearly see we have the old object.

        self.page = result[0] as! NSManagedObject

        // And here I can clearly see that a new object was set.



        self.tableView.reloadData()
        self.view.setNeedsDisplay()
    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }
}

此方法位于我的UIViewController中,其设置如下:

import UIKit
import CoreData

class PageViewController: UIViewController, UITableViewDelegate, UINavigationBarDelegate, UITableViewDataSource {

// Mark: Properties

var page:             NSManagedObject!
var tableView       = UITableView()
var toolBar         = UIToolbar()

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.frame                     = CGRectMake(0, 0, view.frame.width, view.frame.height - 50)
    tableView.estimatedRowHeight        = 200
    tableView.rowHeight                 = UITableViewAutomaticDimension
    tableView.scrollEnabled             = true
    tableView.userInteractionEnabled    = true
    tableView.delegate                  = self
    tableView.dataSource                = self
    tableView.tableHeaderView           = containerView

    self.view.addSubview(tableView)

为什么我的tableView没有重新加载新数据?

更新

根据@Koder和@Simon的建议,我更新了我的代码..但UI仍然没有更新:

func nextPage(sender: UIBarButtonItem) {
    let currentChapter = page.valueForKey("chapter") as! Int
    let appDelegate    = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let fetchRequest   = NSFetchRequest(entityName: "Page")
    fetchRequest.predicate = NSPredicate(format: "(chapter = %d)", currentChapter + 1)
    do {
        let result = try managedContext.executeFetchRequest(fetchRequest)
        self.page = result[0] as! NSManagedObject
        dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
            self.view.setNeedsDisplay()
        }

    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }
}

根据LucaD的推荐,我还会包含我的numberOfRowsnumberOfSections

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.total
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

2 个答案:

答案 0 :(得分:3)

您应该在主线程上触发所有UI刷新代码。 要更新tableView,请尝试从后台线程触发以下内容:

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

此代码块将在主线程上异步执行。

答案 1 :(得分:2)

我怀疑你需要将代码移到主线程中:在后台线程中执行的UI更改不会更新屏幕。试试这个:

dispatch_async(dispatch_get_main_queue())
{
    self.tableView.reloadData()
    self.view.setNeedsDisplay()
}

西蒙