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

Ios .reloadData()和.setNeedsDisplay()将被忽略,ios,swift,uitableview,Ios,Swift,Uitableview,我从点击此方法的工具栏创建了分页工具: func nextPage(sender: UIBarButtonItem) { let currentChapter = page.valueForKey("chapter") as! Int let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let managedContext = appDelegate.managed

我从点击此方法的工具栏创建了分页工具:

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)
    }
}
此方法位于设置如下的my 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的建议,我更新了我的代码。。但用户界面仍然没有更新:

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)
    }
}
根据卢卡德的建议,我还将包括我的
numberOfRows
numberOfSections

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

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

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

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

Simon

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

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

Simon

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

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

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

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

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


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

您对
的意思是不重新加载其新数据。模型是相同的,或者,即使模型不相同,数据源的方法也不会被调用?@LucaD我的意思是UI没有使用新内容更新。
页面
属性肯定设置为新数据。调用了这两个方法,但UI保持不变。
numberOfRowsInSection
numberOfSection
方法被调用?@LucaD是的,我更新了上面的答案。
的意思是不重新加载新数据。模型是相同的,或者,即使模型不相同,数据源的方法也不会被调用?@LucaD我的意思是UI没有使用新内容更新。
页面
属性肯定设置为新数据。调用了这两个方法,但UI保持不变。
numberOfRowsInSection
numberOfSection
方法被调用?@LucaD是的,我更新了上面的答案。有趣。但是我该把这个放在哪里呢?我试着把它放到我的fetchRequest和其他方法中,但我可能还不理解这个方法。非常感谢您的推荐,只需将现有代码包装在my
dispatch\u async
中即可。这使得您调用的方法在主线程中执行,而主线程正是UI更改需要发生的地方。谢谢你的支持。我更新了我上面的答案,我认为这是你的建议,但它似乎仍然不起作用。也许我没有正确地实现它?无论如何谢谢你!有趣。但是我该把这个放在哪里呢?我试着把它放到我的fetchRequest和其他方法中,但我可能还不理解这个方法。非常感谢您的推荐,只需将现有代码包装在my
dispatch\u async
中即可。这使得您调用的方法在主线程中执行,而主线程正是UI更改需要发生的地方。谢谢你的支持。我更新了我上面的答案,我认为这是你的建议,但它似乎仍然不起作用。也许我没有正确地实现它?无论如何谢谢你!非常感谢你的帮助。我更新了我的答案,我认为这是你想法的正确实现,但它仍然不起作用。我应该补充说,
numberOfRowsInSection
cellforrowatinexpath
确实被调用了,所以我假设
reloadData()
函数正在工作。。但是用户界面中的内容实际上没有更新。啊,好的,我知道了。这是因为我不是直接写数据,而是在别处硬编码。谢谢非常感谢你的帮助。我更新了我的答案,我认为这是你想法的正确实现,但它仍然不起作用。我应该补充说,
numberOfRowsInSection
cellforrowatinexpath
确实被调用了,所以我假设
reloadData()
函数正在工作。。但是用户界面中的内容实际上没有更新。啊,好的,我知道了。这是因为我不是直接写数据,而是在别处硬编码。谢谢