2013-11-28 82 views
1

我想製作可摺疊/可展開的tableview,其中我有兩個標題產品和服務,每個包含10+包含自定義單元格的對象,其中包含左側的複選標記和標籤。我檢查了教程,但其中大部分都使用.xib,而我的項目基於故事板。 我查看這些教程,任何人都可以請幫我關於這個。ios故事板上的可摺疊/可擴展桌面

https://www.cocoacontrols.com/controls/collapseclick 
https://www.cocoacontrols.com/controls/ratreeview 
https://www.cocoacontrols.com/controls/combobox-for-uitableview 

回答

0
// 
// ViewController.h 
// expandableTV 

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    NSMutableIndexSet *expandedSections; 
} 
@property (weak, nonatomic) IBOutlet UITableView *tbl_expandablecategory; 

@end 

// 
// ViewController.m 
// expandableTV 
// 


#import "ViewController.h" 
#import "expandableTC.h"` 

@interface ViewController() 
{ 
    NSArray *jsonArray; 
    BOOL currentlyExpanded; 
} 

@end 

@implementation ViewController 
@synthesize tbl_expandablecategory; 
NSMutableArray *arr_categorymenumodel; 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    arr_categorymenumodel=[[NSMutableArray alloc] init]; 
    if (!expandedSections) 
    { 
     expandedSections = [[NSMutableIndexSet alloc] init]; 
    } 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 6;//[arr_categorymenumodel count]; 
} 
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section 
{ 
    return YES; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if ([self tableView:tableView canCollapseSection:section]) 
    { 
     if ([expandedSections containsIndex:section]) 
     { 
      return 2; 
     } 
    } 
    return 1; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row==0) 
    { 
     return 40; 
    } 
    return 270; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    // Configure the cell... 

    if (!indexPath.row) 
    { 
     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     cell.textLabel.text [email protected]"Room ";// [NSString stringWithFormat:@"%@",[[arr_categorymenumodel objectAtIndex:indexPath.section] valueForKey:@"CategoryName"]]; 
     // cell.backgroundColor=[UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1.0]; 
     cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:20.0]; 
     if (currentlyExpanded) 
     { 



     } 
     cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"rightarrow.png"]]; 
     return cell; 
    } 
    else 
    { 
     expandableTC *cell = [tableView dequeueReusableCellWithIdentifier:@"Customcell"]; 

     return cell; 
    } 


} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self tableView:tableView canCollapseSection:indexPath.section]) 
    { 
     if (!indexPath.row) 
     { 
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
      [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

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

      NSMutableArray *tmpArray = [NSMutableArray array]; 

      if (currentlyExpanded) 
      { 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
       [expandedSections removeIndex:section]; 
       cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"rightarrow.png"]]; 

      } 
      else 
      { 
       [expandedSections addIndex:section]; 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
       cell.accessoryView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"downarrow.png"]]; 
      } 

      for (int i=1; i<rows; i++) 
      { 

       NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section]; 
       [tmpArray addObject:tmpIndexPath]; 
      } 

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

      } 
      else 
      { 
       [tableView insertRowsAtIndexPaths:tmpArray 
           withRowAnimation:UITableViewRowAnimationFade]; 

      } 

     } 
    } 
} 



@end