ios - Detect when second tableview cell comes into view - then perform an action -
i've got uitableview
in uiviewcontroller
, filter button. hide filter button when user starts scroll down list. want show filter button when second cell (index 1
) visible. however, can't desired effect, can appear when reaches top. here code when reaches top (i show filter button again).
//determines when tableview scrolled -(void) scrollviewdidscroll:(uiscrollview *)scrollview { cgpoint currentoffset = scrollview.contentoffset; //tableview scrolled down if (currentoffset.y > self.lastcontentoffset.y) { //if showfilter button visible i.e alpha greater 0 if (self.showfilter.alpha==1.0) { [uiview animatewithduration:1 animations:^{ //hide show filter button self.showfilter.alpha=0.0; //i adjust frames of tableview here }]; } } //determines if scrolled top if (self.showfilter.alpha==0.0 && currentoffset.y==0) { [uiview animatewithduration:0.3 animations:^{ //show filter button self.showfilter.alpha=1.0; //i adjust frames of tableview here }]; } self.lastcontentoffset = currentoffset; }
i have tried:
if (self.showfilter.alpha==0.0 && currentoffset.y<160)
but doesn't work desired effect tableview jumps off screen. there way desired effect?
a previous answer suggested should check if cell visible every time table view scrolls (in scrollviewdidscroll:
). however, should not this. approach affect performance of table view since check must performed every single time table view scrolls.
instead, need check every time new cell become visible implementing uitableviewdelegate
method:
- (void)tableview:(nonnull uitableview *)tableview willdisplaycell:(nonnull uitableviewcell *)cell forrowatindexpath:(nonnull nsindexpath *)indexpath { if ([indexpath isequal:[nsindexpath indexpathforrow:1 insection:0]]) { // perform action } }
in swift write following:
func tableview(tableview: uitableview, willdisplaycell cell: uitableviewcell, forrowatindexpath indexpath: nsindexpath) { if indexpath == nsindexpath(forrow: 1, insection: 0) { // perform action } }
in either case don't forget set uiviewcontroller
subclass delegate
of uitableview
.
Comments
Post a Comment