简体   繁体   中英

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

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?

Here I reload whole 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. 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. You should not add observer in cellForRowAtIndexPath: method.

Update: improvement :

Add observers in only custom cells where you need to change Label.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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