在 UITableView 中添加动画部分?

Posted

技术标签:

【中文标题】在 UITableView 中添加动画部分?【英文标题】:Add section with animation in UITableView? 【发布时间】:2012-12-01 05:11:03 【问题描述】:

我在 iPhone 应用程序中工作,使用 UITableView 开发我的应用程序。我有 3 个部分,每个部分有 1 行,当用户在 Section1 中按下 UIButton 调用 B1 方法以在此表视图中添加一个部分和行时。它工作正常。

我已尝试添加动画(用于添加更多部分和行)。

  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:0.2];

但动画所有行和部分,但我想在添加部分和行中添加动画。 请问这个怎么处理?

提前致谢

我试过了:

- (void)viewDidLoad

    [super viewDidLoad];
     ExpandActive = NO;
    self.title=@"Expand Tables";

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    if(ExpandActive == YES)
    
        return 4; 
    
    else
    
         return 3;
    



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

    // Return the number of rows in the section.
    return 1;


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




    if(ExpandActive == YES)
    
        if(section == 0)
        
            return @"Header1";
        
        else  if(section == 1)
        
             return @"Header1";
        
        else if(section == 2)
        
             return @"Header1";    
        
        else if(section == 3)
        
             return @"Header1";
        

    
    else
    
        if(section == 0)
        
             return @"Header1";
        
        else  if(section == 1)
        
             return @"Header1";
        
        else if(section == 2)
        
             return @"Header1";
        
       


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

    return 30;


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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    

    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.userInteractionEnabled=YES;

    if(ExpandActive == YES)
    
        if(indexPath.section == 0)
        
            UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
            bg.backgroundColor = [UIColor clearColor];
            bg.userInteractionEnabled = YES;

            UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn setTitle:@"Yes" forState:UIControlStateNormal];
            btn.frame = CGRectMake(10, 5, 120, 40);
            [btn addTarget:self action:@selector(B1) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn];

            UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn1 setTitle:@"No" forState:UIControlStateNormal];
            btn1.frame = CGRectMake(180, 5, 120, 40);
            [btn1 addTarget:self action:@selector(B2) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn1];
            cell.backgroundView = bg;
            [cell.contentView addSubview:bg];
        
        else if(indexPath.section == 1) 
           
            text = [[UITextView alloc]initWithFrame:CGRectMake(0,0, 320, 50)];
            text.text=@"sdadd";
            [cell.contentView addSubview:text];
        
        else if(indexPath.section == 2) 
        
            UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
            bg.backgroundColor = [UIColor clearColor];
            bg.userInteractionEnabled = YES;

            UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn setTitle:@"Yes" forState:UIControlStateNormal];
            btn.frame = CGRectMake(10, 5, 120, 40);
            [btn addTarget:self action:@selector(B5) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn];

            UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn1 setTitle:@"No" forState:UIControlStateNormal];
            btn1.frame = CGRectMake(180, 5, 120, 40);
            [btn1 addTarget:self action:@selector(B6) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn1];
            cell.backgroundView = bg;
            [cell.contentView addSubview:bg];
        
        else if(indexPath.section == 3) 
        
            UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
            bg.backgroundColor = [UIColor clearColor];
            bg.userInteractionEnabled = YES;

            UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn setTitle:@"Yes" forState:UIControlStateNormal];
            btn.frame = CGRectMake(10, 5, 120, 40);
            [btn addTarget:self action:@selector(B5) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn];

            UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn1 setTitle:@"No" forState:UIControlStateNormal];
            btn1.frame = CGRectMake(180, 5, 120, 40);
            [btn1 addTarget:self action:@selector(B6) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn1];
            cell.backgroundView = bg;
            [cell.contentView addSubview:bg];
        
    
    else
    
        if(indexPath.section == 0)
        
            UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
            bg.backgroundColor = [UIColor clearColor];
            bg.userInteractionEnabled = YES;

            UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn setTitle:@"Yes" forState:UIControlStateNormal];
            btn.frame = CGRectMake(10, 5, 120, 40);
            [btn addTarget:self action:@selector(B1) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn];

            UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn1 setTitle:@"No" forState:UIControlStateNormal];
            btn1.frame = CGRectMake(180, 5, 120, 40);
            [btn1 addTarget:self action:@selector(B2) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn1];
            cell.backgroundView = bg;
            [cell.contentView addSubview:bg];
        
        else if(indexPath.section == 1) 
        
            UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
            bg.backgroundColor = [UIColor clearColor];
            bg.userInteractionEnabled = YES;

            UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn setTitle:@"Yes" forState:UIControlStateNormal];
            btn.frame = CGRectMake(10, 5, 120, 40);
            [btn addTarget:self action:@selector(B3) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn];

            UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn1 setTitle:@"No" forState:UIControlStateNormal];
            btn1.frame = CGRectMake(180, 5, 120, 40);
            [btn1 addTarget:self action:@selector(B4) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn1];
            cell.backgroundView = bg;
            [cell.contentView addSubview:bg]; 
        
        else if(indexPath.section == 2) 
        
            UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
            bg.backgroundColor = [UIColor clearColor];
            bg.userInteractionEnabled = YES;

            UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn setTitle:@"Yes" forState:UIControlStateNormal];
            btn.frame = CGRectMake(10, 5, 120, 40);
            [btn addTarget:self action:@selector(B5) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn];

            UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [btn1 setTitle:@"No" forState:UIControlStateNormal];
            btn1.frame = CGRectMake(180, 5, 120, 40);
            [btn1 addTarget:self action:@selector(B6) forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:btn1];
            cell.backgroundView = bg;
            [cell.contentView addSubview:bg];
        
    

    return cell;


-(void)B1


    NSLog(@"B1");
    ExpandActive = YES;
    [text removeFromSuperview];
    [self.tableView reloadData];



-(void)B2

    NSLog(@"B2");
    ExpandActive = NO;
    [self.tableView reloadData];
    [text removeFromSuperview];

【问题讨论】:

【参考方案1】:

您似乎想将新部分添加为第 1 部分。像这样更新您的两个按钮方法:

-(void)B1 
    NSLog(@"B1");
    ExpandActive = YES;

    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation: UITableViewRowAnimationFade];


-(void)B2 
    NSLog(@"B2");
    ExpandActive = NO;

    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation: UITableViewRowAnimationFade];

随意使用不同的动画。

顺便说一句 - 你有很多重复的代码。您需要消除所有重复的代码。

这是一个例子:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    NSInteger effectiveSection = section;
    if (ExpandActive && effectiveSection > 1) 
        effectiveSection++;
    

    switch (effectiveSection) 
        case 0:
            return @"Fixed Section 1";
        case 1:
            return @"Optional Section";
        case 2:
            return @"Fixed Section 2";
        case 3:
            return @"Fixed Section 3";
    

    return nil; // this shouldn't be reached but makes the compiler happy

另外,如果你有一个固定的高度,不要实现heightForRowAtIndexPath 方法。这是非常低效的。相反,在表格视图上设置rowHeight 属性。一个很好的地方是viewDidLoad。只需设置一次即可完成。

还有一个 - 无需在表格单元格或视图上设置 userInteractionEnabled。默认情况下启用。对于 UILabelUIImageView 之类的内容,它已被禁用。

【讨论】:

【参考方案2】:

使用 UITableView 的– insertSections:withRowAnimation: 方法。您还必须处理 -numberOfSectionsInTableView: 方法中返回的部分数。

【讨论】:

以上是关于在 UITableView 中添加动画部分?的主要内容,如果未能解决你的问题,请参考以下文章

在 uitableview 底部创建一个文本字段并发送按钮,例如 Instagram 评论部分

UITableView 单元格折叠动画看起来很糟糕

动画将新行插入 UITableView 的新部分

用部分动态填充分组的 uitableView

UITableView 动画在部分之间滚动,同时临时删除过多的单元格以简化动画

在 UITableView tableViewHeader 中添加/删除 UISearchBar 的动画