2012-12-01 10 views
0

저는 iPhone 응용 프로그램에서 작업하고 있습니다. UITableView를 사용하여 응용 프로그램을 개발하고 있습니다. 나는 3 섹션 및 각 섹션에 1 행, 사용자가 섹션 1에서 UIButton을 눌러 B1 메서드를 호출하여 하나의 섹션과 행을이 테이블 뷰에 추가 할 때 사용합니다. 그것의 일 벌금.UITableView에서 애니메이션이 포함 된 섹션을 추가 하시겠습니까?

나는 하나의 섹션과 행을 추가하기 위해 애니메이션을 추가하려고 시도했다.

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

그러나 모든 행과 섹션에 애니메이션을 적용하지만 그 당시에는 섹션 및 행 추가에 애니메이션을 추가하고 싶습니다. 처리 방법이 도움이 되셨습니까? 나는이 시도 사전

감사 : jQuery과의

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ExpandActive = NO; 
    [email protected]"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)]; 
      [email protected]"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과 같이 사용할 수 없습니다.

+0

답장을 보내 주셔서 감사합니다. – SampathKumar

0

사용 – insertSections:withRowAnimation: 방법. 또한 -numberOfSectionsInTableView: 메소드에서 반환 된 섹션 수를 처리해야합니다.

관련 문제