【问题标题】:UITableView empty after a reloadData() from NSNotificationCenter来自 NSNotificationCenter 的 reloadData() 后 UITableView 为空
【发布时间】:2015-06-17 09:42:16
【问题描述】:

我的 UITableView 有问题。它总是空的。

摘要: 我有一个模型课。在我的 UITableViewController 中的 viewDidLoad 方法中实例化模型后,它将从我的服务器异步获取一个 JSON 结构。此数据将解析为我的对象。

我的 UITableViewController 也有一个带有选择器方法的 NSNotification Observer。如果模型类发布通知(如果模型完成从服务器请求和解析数据,则发布通知),将触发此选择器方法。

选择器方法将在调度主队列块中执行“self.tableView.reloadData()”。

调试器显示对象不是 nil 而是 tableView 是空的。我不知道为什么 tableview 是空的。

来自我的 UITableViewController 的部分:

import UIKit

类 BillingPlanTableViewController: UITableViewController {

@IBOutlet weak var menuButton: UIBarButtonItem!
var budgetBillingPlansModel: BudgetBillingPlanModel!

// MARK: - Lifecycle
override func viewDidLoad() {
    super.viewDidLoad()

    //Menu gesture recognizer
    if self.revealViewController() != nil {
        menuButton.target = self.revealViewController()
        menuButton.action = "revealToggle:"
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

    //initialize BudgetBillingPlansModel
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "initBbPDone:",name:"initBbPDone", object: nil)
    budgetBillingPlansModel = BudgetBillingPlanModel()
    println(budgetBillingPlansModel)
}


// MARK: - Custom Methods
func initBbPDone(notification: NSNotification){
    //reload tableview: from in thread: Zu diesem Zeitpunkt ist mein Objekt gefüllt.
    dispatch_async(dispatch_get_main_queue(),{
        self.tableView.reloadData()
    });



}


// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    if(self.budgetBillingPlansModel.checkBbPisReady()){
        return self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray().count
    }else{
        return 0
    }

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(self.budgetBillingPlansModel.checkBbPisReady()){
        var numberOfRowsInSection: Int = 0

        for(var index = 0; index < self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray().count; index++){
            if(index == section){
                numberOfRowsInSection = self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray()[index].getSubPlan().count
            }
        }

        return numberOfRowsInSection
    }else{
        return 0
    }

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("budgetBillingPlans", forIndexPath: indexPath) as! BudgetBillingPlanTableViewCell


    if(self.budgetBillingPlansModel.checkBbPisReady()){
        //section == bbP Array
        for(var i = 0; i < self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray().count; i++){
            if(i == indexPath.section){
                //row == subPlans Array
                let subPlans: [SubPlan] = self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray()[i].getSubPlan()
                for(var j = 0; j < subPlans.count; j++){
                    if(j == indexPath.row){
                        cell.ibo_header.text = "\(self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray()[i].getSubPlan()[j].getSbbpDivisionName())"
                        cell.ibo_budgetAmount.text = "\(self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray()[i].getSubPlan()[j].getSbbpActAmount())"
                        cell.ibo_contract.text = "\(self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray()[i].getSubPlan()[j].getSbbpContractNumber())"
                        cell.ibo_date.text = (self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray()[i].getFirstAdjustmentDate()) +  " " + (self.budgetBillingPlansModel.getBudgetBillingPlanModel()!.getBbpArray()[i].getbbpCycleName())
                    }
                }
            }
        }
    }else{

    }

    return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 150
}

}

我的模型类的零件:

class BudgetBillingPlanModel: NSObject {
var budgetBillingPlan: BudgetBillingPlan!
var bbPisReady: Bool!
//getBudgetBillingPlans
private func getBudgetBillingPlans(){
    Alamofire.request(.GET, Constants.service_getBudgetBillingPlans)
        .responseString { (request, response, jsonString, error) in
            println(error)// error ist im debugger-Zeitpunkt == nil
            self.budgetBillingPlan = self.parseServerResponse(jsonString!)
            self.setbbPisReady()
    }
}
//Notifications
private func setbbPisReady(){
    self.bbPisReady = true
    //object controller has to implement the notification method with "initBbPDone"
    NSNotificationCenter.defaultCenter().postNotificationName("initBbPDone", object: nil)
}
//check status
func checkBbPisReady()->Bool{
    if(self.bbPisReady == true){
        return true
    }else{
        return false
    }
}
//getter
func getBudgetBillingPlanModel()->BudgetBillingPlan?{
    if(self.checkBbPisReady()){
        return self.budgetBillingPlan
    }else{
        return nil
    }
}

调试器截图:

补充: 如果我尝试使用静态单元格初始化 tableView,它将显示静态单元格。但是在 viewDidLoad 中实例化之后,当触发通知时,tableview 是空的。如果我像平移或点击手势那样与空的 tableView 交互,我会收到错误:appdelegate (thread1) 中的 EXC_bad_Access

【问题讨论】:

  • 有趣...您是否检查过数据源 - 您是否获得了正确的单元格数据?我确信行数是可以的,可以通过调试器屏幕截图进行检查。另外我认为您不需要将重新加载数据放在调度主线程中,没有它也可以工作
  • 嗨@NickCatib 感谢您的提示。实际上方法 cellForRowAtIndexPath 不会被执行。在带有 reloadData() 的调度块之后,它只执行 numberOfRowsInSection 和 numberOfSectionsInTableView。所以我无法检查单元格是否会获得正确的数据。
  • heightForRowAtIndexPath 怎么样?那个叫吗?
  • 都不...很奇怪
  • 能否尝试在self.tableView.reloadData()之前设置委托和数据源?以防万一

标签: swift uitableview tableview reloaddata nsnotification


【解决方案1】:

我没有发现 mvc 通信的通知模式有问题,但我认为这是线程的问题。解决方案是我切换到kvc-pattern,现在我的tableView不再是空的了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多