Soorej Babu
Soorej Babu

Reputation: 368

Tableview first section (section 0) is not moving up when table view scroll upwards

First of all, am a fresher developer in iOS and it's in my first project and first issue appeared. My problem explains below.

I have a tableView with two sections. The problem is, when i scroll the tableView upwards, the whole tableView except the first section header moves upwards. But there is no problem when scroll it downwards.

my code is:

//----------------Table view delegate method numberOfSectionsInTableView.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     // Return the number of sections.
     return 2;
}

//----------------Table view delegate method numberOfRowsInSection.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if (section==0)
   {
       return [tableSection1 count];
   }
   else
   {
       return [tableSection2 count];
   }
}

//----------------Table view delegate method titleForHeaderInSection.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   if(section==0)
   {
       return @"Mandatory Details";
   }
   else
   {
       return @"Optional Details";
   }
}

//----------------Table view delegate method cellForRowAtIndexPath.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *simpleTableIdentifier = @"SimpleTableItem";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];

   if(indexPath.section==0)
   {
       // Set the data for this cell:

       cell.textLabel.text = [tableSection1 objectAtIndex:indexPath.row];
       cell.detailTextLabel.text = [tableSection1Subtitle objectAtIndex:indexPath.row];
   }
   if(indexPath.section==1)
   {
       // Set the data for this cell:

       cell.textLabel.text = [tableSection2 objectAtIndex:indexPath.row];
       cell.detailTextLabel.text = [tableSection2Subtitle objectAtIndex:indexPath.row];
   }
   return cell;
}

//---------------Table view delegate method didSelectRowAtIndexPath.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section==0)
    {
        if(indexPath.row==0)
        {
            //....my code here....
        }
        if(indexPath.row==1)
        {
            //....my code here....
        }
        if(indexPath.row==2)
        {
            //....my code here....
        }
        if(indexPath.row==3)
        {
            //....my code here....
        }
    }
    if(indexPath.section==1)
    {
        if(indexPath.row==1)
        {
            //....my code here....
        }
        if(indexPath.row==2)
        {
            //....my code here....
        }
        if(indexPath.row==3)
        {
            //....my code here....
        }
    }
}

So here from my code, the section 0 header ie, "Mandatory Details", is not moving upwards according to the entire tableView moves. Section 0 stops at the top of the view and the rest of tableView moves upwards even after the top of the view. But there is no other problems for any other components in the table view even for section 0 if we scroll down.

Please help me. Its looks like a bug to everyone. So i am getting fired from my leader. Kindly help me.

Upvotes: 1

Views: 1734

Answers (2)

Nishant
Nishant

Reputation: 12617

Solution 1: You can change the tableview style to UITableViewStyleGrouped (can do it from interface) in order to achieve what you want.

Solution 2: You can use this tweak:

CGFloat dummyViewHeight = 40;
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
self.tableView.tableHeaderView = dummyView;
self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);

Section headers will now scroll like a cell.

Upvotes: 3

Charmi Gheewala
Charmi Gheewala

Reputation: 828

You may need to change the default behaviour of table header

You can refer this : Change Default Scrolling Behavior of UITableView Section Header

Upvotes: 1

Related Questions