Objective-C : Always display 3 rows in UITableView

After scrolling i want my TableView to display like this :

not like this :

and i have set my rowHeight = tableViewHeight / 3:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CGRectGetHeight(self.tableView.frame)/3;
}

Upvotes: 0

Views: 121

Answers (2)

Jamshed Alam
Jamshed Alam

Reputation: 12844

Follow this steps:

In .h , declare

NSMutableArray  *arrayForBool;
NSMutableArray *arrPastOrder;

In .m ,

- (void)viewDidLoad {

arrPastOrder=[[NSMutableArray alloc] init];

int range=1+arc4random()%10;
for(int i=0;i<range;i++)
{
    NSMutableArray *arrinside=[[NSMutableArray alloc] init];


    for(int j=0;j<3;j++)
    {
        if(j==0)
            [arrinside addObject:[NSString stringWithFormat:@"Some Header %i",i]];
        else
            [arrinside addObject:[NSString stringWithFormat:@"Subindex %i",j]];

    }

    [arrPastOrder addObject:arrinside];

}

arrayForBool=[[NSMutableArray alloc]init];

for (int i=0; i<[arrPastOrder count]; i++)
{
    [arrayForBool addObject:[NSNumber numberWithBool:YES]];
}
  }



#pragma mark -
#pragma mark TableView DataSource and Delegate Methods

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {

   if ([[arrayForBool objectAtIndex:section] boolValue])
    {
    return  [[arrPastOrder objectAtIndex:section] count]-1;
    }
   else
    return 0;

   }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
   {


    static NSString *cellid=@"hello";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];

   if (cell==nil)
   {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
      }


BOOL manyCells  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];

/********** If the section supposed to be closed *******************/
if(!manyCells)
{
    cell.backgroundColor=[UIColor clearColor];

    NSLog(@"cellForRowAtIndexPath---if");

    cell.textLabel.text=@"";
}
/********** If the section supposed to be Opened *******************/
else
{

    //cell.textLabel.text=[NSString stringWithFormat:@"%@ %ld",[sectionTitleArray objectAtIndex:indexPath.section],indexPath.row+1];


    //  ic_keyboard_arrow_up_48pt_2x  ic_keyboard_arrow_down_48pt_2x.png

    cell.textLabel.text=[NSString stringWithFormat:@"%@",[[arrPastOrder objectAtIndex:indexPath.section] objectAtIndex:indexPath.row+1]];


}

cell.textLabel.textColor=[UIColor blackColor];

return cell;

}


 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
    return [arrPastOrder count];

  }

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   {


    NSLog(@"%li--%li",(long)indexPath.section, (long)indexPath.row);

  }


    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
      {
       if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
         return 40;
      }
        return 0;

       }

     - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
     {
          return 100;
     }

   #pragma mark - Creating View for TableView Section

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
     {

UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 540,100)];
sectionView.backgroundColor=[UIColor whiteColor];
sectionView.tag=section;

UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 530, 40)];
viewLabel.backgroundColor=[UIColor whiteColor];
viewLabel.textColor=[UIColor blackColor];
viewLabel.font=[UIFont boldSystemFontOfSize:20];
viewLabel.text=[NSString stringWithFormat:@"%@",[[arrPastOrder objectAtIndex:section] objectAtIndex:0]];
[sectionView addSubview:viewLabel];



/********** Add UITapGestureRecognizer to SectionView   **************/

UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[sectionView addGestureRecognizer:headerTapped];

return  sectionView;


 }


 #pragma mark - Table header gesture tapped

 - (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];    

   //Get which section is open, from here Set the value of arrafool. and expand collapse. Normally whole table is expanded. 


 }

Just run it. You will get the same structure. happy Coding !!

Upvotes: 0

Lorenzo
Lorenzo

Reputation: 1875

There are a couple things you need for this. Firstly, you need to detect when the table view has stopped scrolling: How to know exactly when a UIScrollView's scrolling has stopped?

Next, you need to set the scroll view position: How can I set the scrolling position of an UIScrollView?

The position you're going to want to set it might be the tableview contentOffset - (contentOffset % rowHeight) to move it up to show the row partially shown at the top.

Upvotes: 1

Related Questions