2014-11-29 2 views
-1

2 섹션으로 하나의 tableview을 만들었고 tableview ..에있는 데이터의 배열을 표시했습니다. 이제 섹션을 확장하고 축소하려고합니다 ... 저는 초급자입니다. 이것은 내 cellForRowAtIndexPath 및 arraytable입니다확장 및 iOS에서 tableview에 대한 방법을 축소

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [arraytable count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellitem = @"simpletableitem"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellitem]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellitem]; 
    } 

    cell.textLabel.text = [arraytable objectAtIndex:[indexPath row]]; 

    return cell; 
} 

의 tableview 섹션을 확장 및 축소에 대한 샘플 코드는 ... 내가 한 부하 여기

arraytable = @[@"hari12",@"narayanan",@"Praba",@"Deepak",@"Sailesh",@"Ram charan"]; 
+0

제발 간단한 설명을 해주세요. 제게 많은 복잡한 웹 사이트를 검색했습니다. –

+0

제 머리 꼭대기에서'tableView : numberOfRowsInSection :'을 수정하여'arraytable.count '(확장) 또는 '0'(접힌 상태)으로 표시됩니다. 그래도 더 나은 방법이 될 수 있습니다 ... – AMI289

+0

나는 그것을 시도해 주셔서 감사합니다 ... –

답변

0

내가 만든 한 샘플 코드를 달성하는 것을보기로주고 내 배열입니다 당신이 원하는.

이 작업을 수행하는 방법은 많지 않으므로 이것을 일반적인 지침으로 사용할 수 있습니다.

위 예제에서 두 섹션으로 된 테이블 뷰가 있음을 알았으므로 샘플 코드에서 사용합니다.

아래를 구현하는 데 더 좋은 방법이있을 수 있지만 이는 내 머리 꼭대기에있는 것이므로 매우 단순하고 간단하다고 생각합니다.
또한 아래 코드에서 설명을 주석으로 포함 시켰습니다. 그리고 section1DataArray (당신이 뭔가를 사용하는 경우, 같은 self.tableView 등) 당신은 아마 당신의 코드에 맞게 일부 변수의 이름을 변경해야합니다
주, section2DataArray

// I've declared 2 BOOL properties to hold whether each section is visible or not 
@interface ViewController() 
@property (nonatomic) BOOL section1visible, section2visible; 
@end 

-(void)viewDidLoad { 
    // After creating the table view, I've set the footer view frame to CGRectZero, 
    // so when a table view is collapsed you won't have the table columns 'bounds' 
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame: CGRectZero]; 
} 

// Then I've created a custom header for each table since I've wanted to make the header 
// name a button which collapse/expand the table view. 
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    // Here I've set the height arbitrarily to be 50 points 
    return 50; 
} 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    // Change the below frame to fit your needs. In my example, I've used a table view 
    // to be at the width of the screen, and the height of 50 points (as we've set above) 
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)]; 
    // Then create a button 
    // I've arbitrarily chosen a size of 100x20 and created a frame to be placed in the 
    // middle of the above header 
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(header.frame.size.width/2 - 50, header.frame.size.height/2 - 10, 100, 20)]; 

    // Now I'm setting the tag (for later use) and title of each button 
    if(section == 0) { // First section 
     [button setTitle:@"Section 1" forState:UIControlStateNormal]; 
     button.tag = 0; 
    } else { // Else, second section, since we only have two 
     [button setTitle:@"Section 2" forState:UIControlStateNormal]; 
     button.tag = 1; 
    } 

    // I've arbitrarily chose to set the button colour to gray colour 
    button.titleLabel.textColor = [UIColor grayColor]; 
    // Then we need to actually add an action to the button 
    [button addTarget:self action:@selector(updateTableView:) forControlEvents:UIControlEventTouchUpInside]; 
    // Then we need to add the button to the header view itself 
    [header addSubview:button]; 

    return header; 
} 

// Then we need to update our tableView:numberOfRowsInSection: to check for our BOOL value 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if(section == 0 && self.section1visible) { 
     return [self.section1DataArray count]; 
    } else if (section == 1 && self.section2visible) { 
     return [self.section2DataArray count]; 
    } else { 
     return 0; 
    } 
} 

// Then we need to create the actual method the button calls 
-(void)updateTableView:(UIButton *)sender { 
    // Check the button tag, to identify which header button was pressed 
    NSInteger section = sender.tag; 

    if(section == 0) { 
     self.section1visible = !self.section1visible; 
    } else { // section == 1 
     self.section2visible = !self.section2visible; 
    } 

    // Use an animation to update the UI to create a 'smooth' expand/collapse 
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade]; 
} 

행운 메이트.