【问题标题】:Custom cell is blank when inserted into TableView. Not blank when the app is closed and reopened自定义单元格插入 TableView 时为空白。应用关闭并重新打开时不为空
【发布时间】:2016-07-21 22:45:04
【问题描述】:

我是 iOS 开发的新手,我正在创建我的第一个应用程序,如果问题真的很糟糕,我很抱歉。我在 Xcode 中使用 Master/Detail 模板并使用 Core Data。我将模板更改为在 tableView 中使用自定义单元格样式。当用户想要添加一些东西时,他们会切换到另一个视图以完成表单。当用户单击 Save 时,表单数据被保存到 Core Data,然后视图返回 MasterView。

我的问题是,当保存表单时,tableView 会更新以显示新单元格,但它会间歇性地为空白tableView。有时这会发生,有时不会发生。在从应用切换器关闭应用并重新打开之前,该单元格将保持空白。

有什么我忘记更改的吗?感谢您的帮助。

以下是我在模板中更改的功能以添加我的自定义单元格样式:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomTableViewCell
    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
    self.configureCell(cell, withObject: object)
    return cell
}

func configureCell(cell: CustomTableViewCell, withObject object: NSManagedObject) {

    let imageData: NSData = object.valueForKey("image") as! NSData
    let picture: UIImage = UIImage(data: imageData)!
    let date: NSDate
    let dateFormatter = NSDateFormatter()
    dateFormatter.locale = NSLocale.currentLocale()


    cell.cellImage.image = picture
    cell.locationNameLabel!.text = object.valueForKey("name")!.description
    cell.categoryLabel!.text = object.valueForKey("category")!.description

    date = object.valueForKey("date")! as! NSDate

    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
    cell.dateLabel!.text = dateFormatter.stringFromDate(date)


}

这是我为 FetchedResultsController 更改的唯一函数

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
    case .Update:
        self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)! as! CustomTableViewCell, withObject: anObject as! NSManagedObject)
    case .Move:
        tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
    }
}

【问题讨论】:

    标签: ios swift uitableview core-data


    【解决方案1】:

    我认为您可能需要在添加单元格后重新加载数据以及当您重新回到表格视图时。您可以使用self.tableView.reloadData() 重新加载整个表格或使用self.tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.None) 重新加载特定单元格。 我希望这会有所帮助:)

    【讨论】:

    • 感谢您的帮助。我试过使用tableView.reloadData(),但问题仍然存在。我还在 viewWillAppear 和 viewDidLoad 方法中放置了重新加载功能,但它们也没有帮助。
    • 你说当你关闭应用并重新打开它时,应用会拉取你之前在表单中输入的正确数据并正确输入到单元格中?
    • 是的,当我关闭应用程序时,信息会显示在单元格中。奇怪的是,如果我不关闭应用程序,我仍然可以单击上面发布的图片中的空白单元格,它会将我带到包含信息的正确详细视图。出于某种原因,它只是不想在我关闭并重新打开之前显示自定义单元格。
    • 是的,如果您按照本教程进行操作,它将解释如何将核心数据项添加到表中:learncoredata.com/…
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多