繁体   English   中英

如何使用通知更改uitableviewcell的自定义uilabel,而无需在iOS Swift中重新加载整个表数据

[英]How to change uitableviewcell's custom uilabel using notification with out reloading whole table data in ios swift

我有一个表视图,我在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)

}

如何在不重新加载整个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 您不应在cellForRowAtIndexPath:方法中添加观察者。

更新:改进

仅在需要更改标签的自定义单元中添加观察者。

暂无
暂无

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

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