Had my first go at implementing infinite scrolling for UITableView's in my iOS app last night, and I noticed I ended up copying/pasting quite a bit of boilerplate from one VC to the next (if I wanted infinite scroll on it).
By the way, I'm fairly new to iOS, so this question is probably more broad than just specifically keeping infinite scroll config DRY...
I have properties like these:
@property(nonatomic) int checkins_page;
@property(nonatomic) int checkins_total;
@property(nonatomic) int checkins_total_pages;
@property(nonatomic) BOOL checkins_load_more;
@property(nonatomic) int watchlist_page;
@property(nonatomic) int watchlist_total;
@property(nonatomic) int watchlist_total_pages;
@property(nonatomic) BOOL watchlist_load_more;
I have two UITableViews in this VC, so that's fun.
Then I have a delegate method:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(scrollView == self.checkinsTableView && !self.checkins_load_more){
return;
}
if(scrollView == self.watchlistTableView && !self.watchlist_load_more){
return;
}
CGFloat height = scrollView.frame.size.height;
CGFloat contentYoffset = scrollView.contentOffset.y;
CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;
if(distanceFromBottom < height)
{
if(scrollView == self.checkinsTableView){
self.checkins_load_more = NO;
if(self.checkins_page < self.checkins_total_pages){
self.checkins_page++;
[self downloadCheckins];
}
}
if(scrollView == self.watchlistTableView){
self.watchlist_load_more = NO;
if(self.watchlist_page < self.watchlist_total_pages){
self.watchlist_page++;
[self downloadWatchlist];
}
}
}
}
Now I'm seeing a lot of this stuff could be abstracted...somewhere...somehow, maybe into a more general purpose infinite scroll VC, but like I'm said, quite new to iOS, so I'm not sure the best way to go about reducing this boilerplate code.
Any advice/pointers would be appreciated on how to clean it up slightly?