Objective-C:在UITableView中始终显示3行

时间:2016-10-16 04:30:00

标签: objective-c uitableview

滚动后,我希望我的TableView显示如下:

  • 3 rows display in my frame

不喜欢这样:

  • 4 rows display in my frame

我设置了rowHeight = tableViewHeight / 3:

{{1}}

2 个答案:

答案 0 :(得分:1)

您需要做几件事。首先,您需要检测表视图何时停止滚动: How to know exactly when a UIScrollView's scrolling has stopped?

接下来,您需要设置滚动视图位置: How can I set the scrolling position of an UIScrollView?

您要设置的位置可能是桌面视图contentOffset - (contentOffset % rowHeight),可以将其向上移动以显示部分显示在顶部的行。

答案 1 :(得分:0)

请按照以下步骤操作:

在.h中,声明

NSMutableArray  *arrayForBool;
NSMutableArray *arrPastOrder;

在.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. 


 }

跑吧。你会得到相同的结构。快乐的编码!!