使用 UITableView 时未调用开关语句大小写



我有一个UITableView TableViewController,视图控制器类有一个名为isFolder的变量。

在整个ViewController中,我使用 switch 语句来确定我应该使用 selectedFolder1selectedObjects 的变量。每个变量都是一个 NSManagedObject 子类,并且具有返回数组的 arrayOfItems 方法。

准备 segue 时,isFolder有一个值,具体取决于要传递的数据。

所有这些都工作正常。问题是当传递数据并且我使用 switch 语句来填充表时。当 switch 语句大小写true用于isFolder时,它不是为表函数调用cellForRowAtIndexPath而是用于numberOfRowsInSection。但是,当我在关闭模态视图后重新加载视图时,数据会出现。

例如

此函数工作正常,并调用开关语句大小写 true。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch isFolder {
    case true: print("no. of cells for folder"); return self.selectedFolder.arrayOfItems.count
    case false: print("no. of cells for object"); return self.selectedObject.arrayOfItems.count
    }
}

但是,以下用于填充表视图单元格的函数不会调用 true 的开关语句大小写。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
    let color = colorWheel()
    cell.selectionStyle = .None
    // Configure the cell...
    cell.backgroundColor = color.white
    switch isFolder {
    case true:
        print("Using selected Folder")
        cell.textLabel?.text = selectedFolder.arrayOfItems[indexPath.row].title
    case false:
        print("Using selected Object")
        cell.textLabel?.text = selectedObject.arrayOfItems[indexPath.row].title
    }
    return cell
}

当情况为假时,一切正常,数据首次显示。如前所述,如果 switch 语句为 true,则表会显示数据,但仅当我关闭模态视图并发布通知以调用 self.table.reloadData() 时。我确实在viewDidLoad中调用表方法reloadData但这没有任何作用。

有人有什么建议吗?

确保在

对 UITableView 对象调用 reloadData() 之前填充数据对象。否则,如果numberOfRowsInSections返回 0,则不会调用 cellForRowAtIndexPath

最新更新