2

I need constantly update data inside cell (it's "published ago timer"). I can add NSTimer event inside cell that updates label every second to actual data, but I think that it will create memory leaks.

As you know when cell is not displayed, it's not loaded into memory, so if user scrolls down than upper cells unloads from memory. But will timers exist permanently?

I can .invalidate() timer if cellView unloads from view/memory, but I don't know what func is called when NSTableCellView (or NSView) is unloaded.

Vasily
  • 3,740
  • 3
  • 27
  • 61

3 Answers3

4

Don't put timers to the cells, put one into your controller that does the update of all visible cells like this:

- (void)timerFired {
    for (UITableViewCell *cell in self.tableView.visibleCells) {
        [cell doNecessaryViewUpdate];
    }
}
Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
  • It's good answer, but it works only for iOS. There is no tableView.visibleCells for Mac OS :(. – Vasily Sep 21 '15 at 13:15
  • maybe reloadData is sufficient.. if this produces bad user experience, you should go with approaches like this http://stackoverflow.com/a/17336068/2660952 . In any case I'd avoid to bring up several timers in cells that are somehow "out of control" in the reuse-flow of table views... – Kai Huppmann Sep 21 '15 at 14:21
0

In your custom cell, use deinit {} to invalidate.

Also, uppers cells are not released from memory, rather dequed when needed. So make sure to reset your timer(if needed) in prepareForReuse of your custom cell.

Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
0

I've found such way to update data in all cells (change your class names for cell and table):

updateDateInCellsTimer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: "updateDateInCells", userInfo: nil, repeats: true)

func updateDateInCells() {
    for var row = 0; row < tableView.numberOfRows; row++ {
        guard let cell = tableView.viewAtColumn(0, row: row, makeIfNecessary: false) as? NSTableCellView else { continue }

        // here you can do anything, cell is your cell reference
    }
}

It's working for NSTableView. If you want to use it at iOS (there is UITableView), use answer of @Kai Huppmann.

Community
  • 1
  • 1
Vasily
  • 3,740
  • 3
  • 27
  • 61