Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide section for UITableView on first load

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.

like image 956
goupilz Avatar asked Dec 30 '25 12:12

goupilz


2 Answers

  • Take a BOOL variable for checking whether to load one section or two section.
  • Make is YES in ViewDidLoad then,for the first time UITableView will load one section.
  • Then when UITableView will be scrolled to top,make that BOOL variable to NO
- (void)viewDidLoad
{
    [super viewDidLoad];

    isFirstTime = YES;
}
  • In datasource method of TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    if (isFirstTime) {
        return 1;
    }
    return 2;
}
  • For checking the 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
   }

}

like image 122
Jayaprada Avatar answered Jan 01 '26 05:01

Jayaprada


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.

like image 26
manujmv Avatar answered Jan 01 '26 06:01

manujmv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!