[英]How to change uitableviewcell's custom uilabel using notification with out reloading whole table data in ios swift
I have a tableview I set timer in ViewDidLoad() as follows 我有一个表视图,我在ViewDidLoad()中设置了计时器,如下所示
self.timer = NSTimer(timeInterval: 10.0, target: self, selector: Selector("fireCellsUpdate"), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
func fireCellsUpdate() {
print("In fireCellsUpdate")
let notification = NSNotification(name: "CustomCellUpdate", object:UILabel())
NSNotificationCenter.defaultCenter().postNotification(notification)
}
and in tableviewcell as follows
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
var lbl_uploadTime = UILabel()
lbl_uploadTime.tag = indexPath.row
lbl_uploadTime.text = "3 hours 2 mins"
cell.contentView.addsubView(lbl_uploadTime)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateCountdownLabel:", name: "CustomCellUpdate", object: nil)
}
How can I update text for lbl_uploadTime without reloading whole tableview? 如何在不重新加载整个tableview的情况下为lbl_uploadTime更新文本?
Here I reload whole tableview 在这里我重新加载整个tableview
func updateCountdownLabel(notification: NSNotification) {
println("broadcast received in cell %@",notification)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableview_DiscoverVideos.reloadData()
})
}
But I want to change only label text without reloading whole tableview. 但是我只想更改标签文本而不重载整个tableview。 Please help me. 请帮我。
I recommend you to subclass UITableViewCell
and add observer in that cell and then update to that UILabel
to whatever you need. 我建议您UITableViewCell
子类,并在该单元格中添加观察者,然后将其更新到您需要的UILabel
。 You should not add observer in cellForRowAtIndexPath:
method. 您不应在cellForRowAtIndexPath:
方法中添加观察者。
Update: improvement : 更新:改进 :
Add observers in only custom cells where you need to change Label. 仅在需要更改标签的自定义单元中添加观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.