2013-02-14 2 views
0

다음 예제 섹션 1과 2는 확장 가능하지만 모든 확장 섹션에는 각각 4 개의 셀이 있습니다. 섹션 1에서 4 행 2. 첫 번째 방법은 행을 지정하는 부분에서 2 개 개의 행을 맞추기 방법 확장하고 나머지의 tableview 대리자와 소스 방법이다4 개의 행과 2 개의 행으로 확장 가능한 두 섹션을 생성하려고합니다. 다음 코드는 각 행이 4 개의 두 개의 확장 가능한 섹션을 생성합니다.

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section 
    { 
     if (section>0 && section<3) return YES; 

     return NO; 
    } 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     // Return the number of sections. 
     return 4; 
    } 





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

    if ([self tableView:tableView canCollapseSection:section]) 
    { 

     if ([expandedSections containsIndex:section]) 
     { 

      NSLog(@"section number:%d",section); 
      return 3; // return rows when expanded 

     } 
      return 1; // only top row showing 

    } 

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


} 



- (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]; 
    } 

    // Configure the cell... 

    if ([self tableView:tableView canCollapseSection:indexPath.section]) 
    { 
     if (!indexPath.row) 
     { 
      // first row 
      cell.textLabel.text = @"Expandable"; // only top row showing 

      if ([expandedSections containsIndex:indexPath.section]) 
      { 
      //some code 
      } 
      else 
      { 
       //some code 
      } 
     } 
     else 
     { 
      // all other rows 
      cell.textLabel.text = @"Some Detail"; 
      cell.accessoryView = nil; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
    } 
    else 
    { 
     cell.accessoryView = nil; 
     cell.textLabel.text = @"Normal Cell"; 

    } 

    return cell; 
} 

답변

0

복귀 numberOfRowsInSection 방법의 행 적절한 수

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if ([self tableView:tableView canCollapseSection:section]) 
    { 
     if ([expandedSections containsIndex:section]) 
     { 
      NSLog(@"section number:%d",section); 
      // -------------- UPDATE HERE -------------- 
      return (indexPath.section == 1 ? 4 : 2); // return rows when expanded 
     } 
     return 1; // only top row showing 
    } 
    // Return the number of rows in the section. 
    return 1; 
} 
관련 문제