I have a UITableView with 2 sections (the 1st for the filters and the 2nd for the data).
I want to hide the 1st section when the view is loaded.
The section appears only when I scroll on top.
I tested with scrollToRowAtIndexPath: but the result is not as expected.
Please help. Thanks.
BOOL variable for checking whether to load one section or
two section.YES in ViewDidLoad then,for the first time UITableView will load one section.BOOL variable to NO- (void)viewDidLoad { [super viewDidLoad]; isFirstTime = YES; }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if (isFirstTime) { return 1; } return 2; }
UITableView to scroll TOP.We will use delegate methods of UIScrollview.UITableView is the subclass of UIScrollView.-(void)scrollViewDidScroll:(UITableView *)tableView{ CGFloat content_Offset = tableView.contentOffset.y; if (content_Offset >= 0 && isFirstTime){ //UITableview is scrolled to top isFirstTime = NO; [self.myTbaleView reloadData]; }else if (content_Offset<0) { //UITableview is draged down }}
You can add a BOOL variable to find out whether it is loading first time or not. Set it true in the viewDidLoad like:
- (void)viewDidLoad
{
[super viewDidLoad];
isFirstTimeLoading = YES;
}
Then in your numberOfSectionsInTableView: delegate method,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (isFirstTimeLoading) {
return 1;
} else {
return 2;
}
}
TableView scrolling to top can be find out using
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
In this method, check whether the row is top row. if yes then make isFirstTimeLoading to NO and reload data. Dont forget to change the data also if necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With