【问题标题】:Objective-C : Always display 3 rows in UITableViewObjective-C:始终在 UITableView 中显示 3 行
【发布时间】:2016-10-16 04:29:59
【问题描述】:

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

不是这样的:

我已经设置了我的 rowHeight = tableViewHeight / 3:

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

【问题讨论】:

    标签: objective-c uitableview


    【解决方案1】:

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

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

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

    【讨论】:

    • 我花了一段时间才弄明白,但在您的指导下,我做到了。
    【解决方案2】:

    按照以下步骤操作:

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

    只需运行它。你会得到相同的结构。快乐编码!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 2014-07-12
      • 2012-04-19
      • 2016-09-09
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      相关资源
      最近更新 更多