2013-03-15 2 views
4

나는 이미 DDMenuController의 도움을 받아 Facebook/Gmail iphone 앱 유형 메뉴가 필요한 앱을 만들고 있습니다. 하지만 이제는 행 중 하나가 5 행 (다른 모든 viewcontroller를 푸시 할 수 있어야하며 클릭 가능해야 함)이있는 다른 tableview를 클릭 할 때 아코디언을 표시해야하는 요구 사항이 있습니다. 몇 가지 코드 샘플을 보았지만 아무 것도 내 요구 사항에 맞지 않는 것처럼 보입니다. 따라서 누군가를위한 더 나은 솔루션이 필요하기를 바랄뿐입니다.UItableview에서 UItableview를 사용하여 아코디언을 만드는 방법은 무엇입니까?

감사합니다,

+4

오초

코드는 꽤 괜찮은 자습서를 제공합니다. 예를 들어 [this] (http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/)와 같이. 확장 또는 축소 테이블 뷰를 검색해보십시오. – SethHB

+0

Swift에서이 아코디언 예제를 볼 수 있습니다 : https://github.com/tadija/AEAccordion 아코디언 효과를 생성하는 데는 코드가 거의없고 (셀을 사용하는 것이 아니라 셀을 사용) 보너스가 있습니다. 또한 다른 XIB 파일에서 XIB 파일을 사용하는 솔루션 (사용자 지정보기를 사용하는 사용자 지정 셀에 유용). – tadija

답변

8

더 나은 솔루션은 here

당신은 샘플 코드를 다운로드 할 수 있습니다 확장 또는

좋은 자습서를 사용할 수의 TableView 섹션

축소입니다 here

샘플 코드

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

    // 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]) 
      { 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp]; 
      } 
      else 
      { 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]; 
      } 
     } 
     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; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self tableView:tableView canCollapseSection:indexPath.section]) 
    { 
     if (!indexPath.row) 
     { 
      // only first row toggles exapand/collapse 
      [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

      NSInteger section = indexPath.section; 
      BOOL currentlyExpanded = [expandedSections containsIndex:section]; 
      NSInteger rows; 

      NSMutableArray *tmpArray = [NSMutableArray array]; 

      if (currentlyExpanded) 
      { 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
       [expandedSections removeIndex:section]; 

      } 
      else 
      { 
       [expandedSections addIndex:section]; 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
      } 

      for (int i=1; i<rows; i++) 
      { 
       NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                   inSection:section]; 
       [tmpArray addObject:tmpIndexPath]; 
      } 

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

      if (currentlyExpanded) 
      { 
       [tableView deleteRowsAtIndexPaths:tmpArray 
           withRowAnimation:UITableViewRowAnimationTop]; 

       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]; 

      } 
      else 
      { 
       [tableView insertRowsAtIndexPaths:tmpArray 
           withRowAnimation:UITableViewRowAnimationTop]; 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp]; 

      } 
     } 
    } 
} 
+0

이것은 확장이 아니며, 그 빌어 먹을 경고 롤 – ChuckKelly

12

내 작품 중 하나에는 아코디언 뷰가 있어야하지만 디렉토리 구조를 열고 닫는 것처럼 여러 수준의 확장 및 축소가 필요합니다.

샘플을 작성하여 원하는 결과를 얻을 수있었습니다. 기본 개념은 동일하지만 deleteRowsAtIndexPath 및 insertRowsAtIndex 경로 만 사용하지만 부모 자식 관계가있는 모델 객체를 만들고 부모 노드가 도청 될 때마다 자식 배열을 기본 배열에로드합니다. 튜토리얼을 작성하는 것이 좋지 않아 샘플 코드를 공유하므로 다른 사용자에게 도움이되기를 바랍니다. 여기 Accordion_git

코드 최적의 여부 만 작동 확실하지 않은이의 SWIFT 버전을 했 업데이트되었습니다. 구글에 여기 Accordion_SWIFT

Accordion

+2

무엇 공유 웹 사이트에 대한 나쁜 선택 .. – Rizon

+2

@Rizon는 자식으로 변경 :) – anoop4real

+1

고마워! 몇 년 전에 중첩 된 tableviews를 사용하여이 컨트롤을 구현했지만 접근 방식이 더 의미가 있습니다. 건배. – codrut

관련 문제