swift - 使用通知中心将值设置为 TableView 中的标签

标签 swift uitableview nsnotificationcenter

我有一个带有自定义单元格的表格 View 。在单元格中,我有一个标签,用于计算该特定单元格中的按钮按下次数。

计数器是一个单独类的一部分:

class List {
    var name = ""
    var description = ""
    var counter: Int = 0

    init(name: String, description: String) {

        self.name = name
        self. description = description    
    } 
}         

var staticList: [List] =
        [ //Lots of names and descriptions.   
        ]

在 viewDidLoad 中,我这样做:

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

以及我尝试过的功能:

 @objc func refreshLbl(cell: MenuCell)
    {
        let indexPath = menuTableView.indexPath(for: cell)
       staticList[(indexPath?.row)!].counter = 0   
    } 
}    

因此,我的目标是在触发 funcfreshLbl 时将所有 counterLabels 中的计数器设置为 0。

最佳答案

只需运行此循环并将计数器的值更新为零:

 for list in staticList{
        list.counter = 0
    }

然后重新加载表格 View :tableView.reloadData()

关于swift - 使用通知中心将值设置为 TableView 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48914251/

相关文章:

ruby - 在 macOS App 中使用非系统 ruby​​ 执行 ruby​​ 脚本

ios - 继承 UIButton 外观的 UITableViewCell 披露指示器

ios - xcode-线程 1 : EXC_BAD_ACCESS

objective-c - 记录 NSNotifications

iphone - 主线程上的 NSOperation 和 NSNotificationCenter

iOS 同步使用状态方法,而不仅仅是对事件使用react

ios - 如何使用拆分和映射方法?

ios - 通过桥接头从 SWIFT 调用 objective-c 函数

swift - 重启 AVAssetWriter

ios - 如何为多个 UITableView 设置属性?