如何更改uitableviewcell';在ios swift中使用通知而不重新加载整个表数据的自定义uilabel
我有一张桌子 我在ViewDidLoad()中设置了计时器,如下所示如何更改uitableviewcell';在ios swift中使用通知而不重新加载整个表数据的自定义uilabel,ios,swift,uitableview,nsnotificationcenter,nsnotification,Ios,Swift,Uitableview,Nsnotificationcenter,Nsnotification,我有一张桌子 我在ViewDidLoad()中设置了计时器,如下所示 self.timer = NSTimer(timeInterval: 10.0, target: self, selector: Selector("fireCellsUpdate"), userInfo: nil, repeats: true) NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes) fun
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)
}
如何在不重新加载整个tableview的情况下更新lbl_uploadTime的文本
在这里,我重新加载整个tableview
func updateCountdownLabel(notification: NSNotification) {
println("broadcast received in cell %@",notification)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableview_DiscoverVideos.reloadData()
})
}
但我只想更改标签文本而不重新加载整个tableview。请帮助我。我建议您将
UITableViewCell
子类化,并在该单元格中添加观察者,然后根据需要更新到该UILabel
。您不应该在cellforrowatinexpath:
方法中添加观察者
更新:改进:
仅在需要更改标签的自定义单元格中添加观察者。您需要一种方法来了解单元格所在的行和区域。然后调用
yourTable.cellforrowatinexpath
。更改特定单元格的lbl\u uploadTime的值,并仅重新加载单元格而不是整个单元格tableview@SaurabhPrajapati我该怎么办that@boidkan如何获取方法func updateCountdownLabel(通知:NSNotification){}中的节?要重新加载哪个UITableviewcell?问题是,如果为每个单元格添加一个观察者,并且所有观察者使用相同的名称,则post将触发每个单元格的观察者。所以这不是最好的解决方案。更新了我的答案。然而,NSNotification并不是一种很好的方法,但正如问题“使用通知”中所问的,这是我的建议。