2011/09/04

實作 TableView Section 展開/收合

iPhone中的UITableViewController變化真的很多!
最近剛好需要實作展開/收合的功能,效果如圖所示:

我是參考Expanding/Collapsing TableView Sections的實作,然後在自己簡化。
一個展開/收合的單位為一個Section,Row0表示Parent,Row1~N表示Child,程式碼中都有註解。



  • testTableViewController.h
#import 

@interface MoodDiaryViewController : UITableViewController {
 /* Store the indexpath which already expanded */
 NSMutableIndexSet *expandedSections;
}
@end


  • testTableViewController.h
#import "testTableViewController.h"

@implementation MoodDiaryViewController

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

 if (!expandedSections)
    {
        expandedSections = [[NSMutableIndexSet alloc] init];
    } 
}


#pragma mark -
#pragma mark Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    /* Return the number of rows in the section. */
 if ([expandedSections containsIndex:section]) {
  /* Return all rows when expanded */
  return 5;
 } else {
  /* Only top row showing */
  return 1;
 }
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d_%d", indexPath.section, indexPath.row];
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 
 if (indexPath.row == 0) {
  /* Parent cell */
  if (cell == nil)
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  
  /* Change UI status */
  if ([expandedSections containsIndex:indexPath.section]) {
   cell.textLabel.text = [NSString stringWithFormat:@"- Section=%d Row=%d", indexPath.section, indexPath.row];
  } else {
   cell.textLabel.text = [NSString stringWithFormat:@"+ Section=%d Row=%d", indexPath.section, indexPath.row];
  }
  
 } else {
  /* Child cell */
  if (cell == nil)
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
  
  cell.textLabel.text = [NSString stringWithFormat:@"Section=%d Row=%d", indexPath.section, indexPath.row];
 }

    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 /* If user choose the parent row */
 if (indexPath.row == 0) {
  NSInteger section = indexPath.section;
  BOOL currentlyExpanded = [expandedSections containsIndex:section];
  NSInteger rows;
  NSMutableArray *arrRows = [NSMutableArray array];
  
  if (currentlyExpanded) {
   /* Child cell for this parent */
   rows = [self tableView:tableView numberOfRowsInSection:section];
   [expandedSections removeIndex:section];
  } else {
   [expandedSections addIndex:section];
   rows = [self tableView:tableView numberOfRowsInSection:section];
  }

  /* Create child index path. Child path start frow index one */
  for (int i = 1; i < rows; i++) {
   NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
   [arrRows addObject:tmpIndexPath];
  }

  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  
  /* Remove of insert above index path into tableview */
  if (currentlyExpanded) {
   [tableView deleteRowsAtIndexPaths:arrRows withRowAnimation:UITableViewRowAnimationTop];
   cell.textLabel.text = [NSString stringWithFormat:@"+ Section=%d Row=%d", indexPath.section, indexPath.row];
  } else {
   [tableView insertRowsAtIndexPaths:arrRows withRowAnimation:UITableViewRowAnimationTop];
   cell.textLabel.text = [NSString stringWithFormat:@"- Section=%d Row=%d", indexPath.section, indexPath.row];
  }
  
 } else { // For choosing child row
  ;
 }
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}
@end


沒有留言:

張貼留言