Has the feedback just disappeared in the UITableView cell?

I have a UITableView with heavy image content. So the scroll is no longer liquid. I want to add a timer to load images while you scroll. I create a timer for each line. If the cell closes the view, I cancel the timer. If not, I disappear in the images.

My question is: is there a callback for a cell coming out of sight? I am reading a document, but I'm not sure if there is anything for my needs.

Thanks for the help!

EDIT: The code I use (this is three20 library, I use custom TTTableItemCell. "_TabBar1.tabItems = item.photos" are resources for finding strings. On the first load, this is normal, because the photos are downloaded asynchronously from the server, but when I I scroll back or reload the view, they all load synchronously, and the scrolling is no longer smooth, especially on 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; } 
+7
source share
4 answers

Ok, I found a way.

Actually there is a callback to find out which cell is about to leave the field 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 exits the view, and therefore first check that my timer has not yet been canceled, and then I canceled it.

+5
source

If you are using iOS 6 and above, simply override this method:

- tableView:didEndDisplayingCell:forRowAtIndexPath:

It will be called when the cell has already left the view, and you will get it and its indexPath.

+8
source

I suggest using the KVO approach:

In your awakeFromNib method (or any method that 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 follows:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualToString:@"hidden"]) { NSLog(@"cell is hidden"); } } 
+1
source

When a UITableView initially displayed, it calls this method once for each row

 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 

after that, the method is called when a new line is required, which is often the result of scrolling to show a new line and pressing the old view from the other end.

So this is the perfect hook to find out which lines are visible. To check which cells are visible, you can call

 - (NSArray *)indexPathsForVisibleRows 

Since tableview is the only thing that contains a link to your cells, before they are processed or freshly prepared, you will not be able to access these timers. I suggest creating an NSMutableDictionary ivar, and when you create your cells add a timer to NSMutableDictionary

 [timersForIndexs setObject:yourTimer forKey:indexPath]; 

Now that you get - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath , you need to do something like

  NSMutableDictionary *tmpDictionary = [timersForIndexs copy]; [tmpDictionary removeObjectsForKeys:[self.tableView indexPathsForVisibleRows]]; NSArray *timers = [tmpDictionary allKeys]; [timers makeObjectsPerformSelector:@selector(invalidate)]; 

I am not in front of xcode, so this is dry encoding, so please let me know if you have any problems.

0
source

All Articles