简体   繁体   English

reloadData() 不更新单元格标签

[英]reloadData() doesn't update a cell label

In my app I have a label that inform the user that a few settings are missing:在我的应用程序中,我有一个标签,通知用户缺少一些设置: 在此处输入图片说明

which is set in:设置在:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let service = (fetchedResultsController?.object(at: indexPath))! as Service
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! OfferedServiceCell
    cell.setupDataCell(service: service)
    if service.employee?.count == 0   || service.price?.price == nil || service.price?.slotDuration == nil {
        cell.infoLabel.text = "attention: you need to complete this service set up!"
    }
    let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell(gr:)))
    cell.addGestureRecognizer(lpGestureRecognizer)
    return cell
}

User can add price and employee proving that service in another tableViewController and the label should go away when all the information needed are saved in core data.用户可以在另一个 tableViewController 中添加价格和员工证明该服务,当所需的所有信息都保存在核心数据中时,标签应该消失。 The problem is that the label red warning goes away only when I reload the viewController.问题是标签红色警告只有在我重新加载 viewController 时才会消失。

THIS IS WHAT I HAVE TRIED SO FAR:这是我迄今为止尝试过的:

  1. Since I am using fetchedResultsController, I have implemented this method:由于我使用的是 fetchedResultsController,所以我实现了这个方法:

     func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { DispatchQueue.main.async { self.tableView.reloadData() } }

    The method gets called as soon as I change core data but the warning is still there (unless the TableViewController is reloaded).一旦我更改核心数据,该方法就会被调用,但警告仍然存在(除非重新加载TableViewController )。 I have also tried to add self.tableView.layoutIfNeeded() and infoLabel.layoutIfNeeded() in OfferedServiceCell .我还尝试在OfferedServiceCell添加self.tableView.layoutIfNeeded()infoLabel.layoutIfNeeded()

  2. Post a notification when the TableViewController where I add price and employee is dismissed:当我添加价格和员工的TableViewController被解雇时发布通知:

     NotificationCenter.default.addObserver(self, selector: #selector(reloadAllData), name: NSNotification.Name(rawValue: "priceListModified"), object: nil)

in reloadAllData I even tried to call performFetch againreloadAllData我什至试图再次调用performFetch

func reloadAllData(){
     DispatchQueue.main.async {
                do {
                    try self.fetchedResultsController?.performFetch()

                } catch {
                    fatalError("Failed to initialize FetchedResultsController: \(error)")
               }

        self.tableView.reloadData()
        self.tableView.layoutIfNeeded()
        }
}

I don't know what else I can do.我不知道我还能做什么。 Thanks for you suggestions.谢谢你的建议。

-----UPDATE--- problem solved: -----更新---问题解决:

As correctly pointed out by Chris Trahey's answer I ended up moving the code where I set up the red label to my custom cell and explicitly hide it when it isn't needed:正如 Chris Trahey 的回答正确指出的那样,我最终将设置红色标签的代码移动到我的自定义单元格,并在不需要时明确隐藏它:

func setupDataCell(service:Service){
    self.service =  service
    if service.service_description != nil {
        serviceName.text = service.service_description
    }

    if service.employee?.count == 0   || service.price?.price == nil || service.price?.slotDuration == nil {
        if service.selected == 0 {
        infoLabel.text = "Drag service to Service Offered to start using it"
        }else{
        infoLabel.text = "attention: you need to complete this service set up!"
        }
    }else{
        infoLabel.isHidden = true
    }
}

and I used prepareForReuse to set the label to nil.我使用 prepareForReuse 将标签设置为 nil。 (still not sure about this but it doesn't work without) (仍然不确定这一点,但没有它就行不通)

override func prepareForReuse() {
    super.prepareForReuse()
    infoLabel.text = nil

}

I suspect that the cell setup routine is not clearing out the label in the event that it is not required.我怀疑在不需要的情况下,单元设置例程没有清除标签。 Therefore, the label is surviving the re-use cycle.因此,标签在重复使用周期中幸存下来。 I would do both of:我会做两个:

  • setupDataCell should not just set the red label properties when it's needed, it should also explicitly hide it when it isn't. setupDataCell不应该只在需要时设置红色标签属性,还应该在不需要时显式隐藏它。
  • implement prepareForReuse on the custom cell class and clear the label在自定义单元格类上实现prepareForReuse并清除标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM