Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cell just disappeared callback?

Tags:

ios

I have a UITableView with heavy images content. So the scrolling is not fluid anymore. I want to add a timer to load the images, while you scroll I create the timer for each row. If the cell quits the view, I cancel the timer. If not I fade in the images.

My question is : is there a callback for a cell going out of view ? I'm reading the doc, but I'm not sure there is anything for my needs.

Thanks for the help !

EDIT: The code I'm using (this is the three20 library, I'm using a custom TTTableItemCell. The "_tabBar1.tabItems = item.photos" is the line hoging resources. On the first load it's okay because the photos are being loaded asynchronously from the server, but when I scroll back or reload the view, they are all loaded synchronously, and the scrolling isn't smooth anymore, especially on an iPhone 3G. :

- (void)setObject:(id)object {
    if (_item != object) {
        [super setObject:object];

        Mission* item = object;

        self.textLabel.text = item.name;
        _tabBar1.tabItems = nil;

        timerFeats = [NSTimer scheduledTimerWithTimeInterval:(0.5f) target:self selector:@selector(updateFeats) userInfo:nil repeats: NO];  
        //_tabBar1.tabItems = item.photos;
  }
}

-(void)updateFeats {
    DLog(@"timer ended");
    Mission* item = self.object;
    self._tabBar1.tabItems = item.photos;
}
like image 378
rnaud Avatar asked Aug 21 '11 10:08

rnaud


People also ask

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.

Is UITableView scrollable?

Smooth Scrolling:- As a result, it affects the smoother scrolling experience. In UITableVIew, Scrolling will be smooth through cell reuse with the help of prepareForReuse() method.

How do I deselect a selected UITableView cell?

How to deselect a UITableViewCell using clearsSelectionOnViewWillAppear. If you set this property to be true the user's selected cell will automatically be deselected when they return to the table view.


3 Answers

If you're using iOS 6 and up, simply override this method:

- tableView:didEndDisplayingCell:forRowAtIndexPath:

It'll be called when the cell has already gone out of the view, and you'll get it and its indexPath.

like image 110
Velociround Avatar answered Sep 20 '22 23:09

Velociround


Alright, I found a way.

There is actually a callback to know what cell is about to get out of view. :

- (void)willMoveToSuperview:(UIView *)newSuperview;

So my code is :

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    if(!newSuperview) {
        DLog(@"timer invalidated");
        if ([timerFeats isValid]) {
          [timerFeats invalidate];
        }

    }
}

If there is no newSuperview the cell is going out of the view and so I verify first that my timer hasn't been invalidated yet, and then I cancel it.

like image 30
rnaud Avatar answered Sep 19 '22 23:09

rnaud


I suggest to use a KVO approach:

On your awakeFromNib method (or whatever method you use to instantiate the cell) add the following:

- (void)awakeFromNib {
    [self addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew context:nil];

    ...
}

Be sure to implement the delegate method for the observer as follow:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if([keyPath isEqualToString:@"hidden"]) {
        NSLog(@"cell is hidden");
    }
}
like image 27
valvoline Avatar answered Sep 21 '22 23:09

valvoline