2014-09-05 2 views
0

У меня есть вид таблицы с разделом и я хотел бы начать представление таблицы со всеми раздела сгруппированы так:Xcode - UITableView уплотняется

Section1 
Section2 
Section3 

Когда я нажмите на заголовок раздела, это расширить ряды

Section1 
Row1 
Row2 
Row3 
Section2 
Section3 
+0

http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/ – iCoder

ответ

0

я просил, чтобы все увидеть, пожалуйста, ответ, если вы не любите, просто комментарий

это для Beginn ERS ....

этот код также refered только от сети, и я изменил некоторые вещи

this link

Creat файл класса ...

ViewController.h файл be

#import <UIKit/UIKit.h> 
#import "oneViewController.h" 
#import "secondViewController.h" 
#import "thirdViewController.h" 
@interface ViewController : UIViewController 
{ 
    oneViewController *one; 
    secondViewController *second; 
    thirdViewController *third; 
} 
@end 

ViewController.m файл должен быть

#import "ViewController.h" 
#import "Cell1.h" 
#import "Cell2.h" 
@interface ViewController()<UITableViewDataSource,UITableViewDelegate> 
{ 
    NSMutableArray *dataList; 

} 
@property (assign)BOOL isOpen; 
@property (nonatomic,retain)NSIndexPath *selectIndex; 

@property (nonatomic,retain)IBOutlet UITableView *expansionTableView; 
@end 

@implementation ViewController 
@synthesize isOpen,selectIndex; 

- (void)dealloc 
{ 
    [dataList release]; 
    dataList= nil; 
    self.expansionTableView = nil; 
    self.isOpen = NO; 
    self.selectIndex = nil; 
    [super dealloc]; 
} 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nil]; 
    if (self) { 
     one=[[oneViewController alloc]init ]; 
     second=[[secondViewController alloc]init]; 
     third=[[thirdViewController alloc]init]; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"array" ofType:@"plist"]; 
    dataList = [[NSMutableArray alloc] initWithContentsOfFile:path]; 
    NSLog(@"%lu",(unsigned long)dataList.count); 
    self.expansionTableView.sectionFooterHeight = 0; 
    self.expansionTableView.sectionHeaderHeight = 0; 
    self.isOpen = NO; 
} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [dataList count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (self.isOpen) { 
     if (self.selectIndex.section == section) { 
      return [[[dataList objectAtIndex:section] objectForKey:@"list"] count]+1; 

     } 

    } 
    return 1; 
} 
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 40; 
} 


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

    NSLog(@"%@",[[dataList objectAtIndex:indexPath.section]objectForKey:@"list"]); 
    if (self.isOpen&&indexPath.section == indexPath.section&&indexPath.row!=0) { 
     static NSString *CellIdentifier = @"Cell2"; 
     Cell2 *cell = (Cell2*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (!cell) { 
      cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0]; 
     } 
     if ([[dataList objectAtIndex:indexPath.section]objectForKey:@"list"]) 
     { 
      NSArray *list = [[dataList objectAtIndex:indexPath.section] objectForKey:@"list"]; 
      cell.titleLabel.text = [list objectAtIndex:indexPath.row-1]; 
      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
      return cell; 
     } 

     return cell; 
    }else 
    { 
     static NSString *CellIdentifier = @"Cell1"; 
     Cell1 *cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (!cell) { 
      cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0]; 
     } 
     NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:@"name"]; 
     cell.titleLabel.text = name; 
     //[cell changeArrowWithUp:([indexPath isEqual:indexPath]?YES:NO)]; 
     if ([indexPath isEqual:indexPath]) 
     { 
      [cell changeArrowWithUp:YES]; 
     } 
     else 
     { 
      [cell changeArrowWithUp:NO]; 
     } 
     return cell; 
    } 

} 


#pragma mark - Table view delegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) { 
     if ([indexPath isEqual:self.selectIndex]) { 
      Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:self.selectIndex]; 
      [cell changeArrowWithUp:YES]; 
      self.isOpen = NO; 
      [self didSelectCellRowFirstDo:NO nextDo:NO]; 
      self.selectIndex = nil; 

     }else 
     { 
      if (!self.selectIndex) { 
       self.selectIndex = indexPath; 
       [self didSelectCellRowFirstDo:YES nextDo:NO]; 
       Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:self.selectIndex]; 
       [cell changeArrowWithUp:NO]; 


      }else 
      { 
       NSIndexPath *tempIndex=indexPath; 
       if (tempIndex) 
       { 
        Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:tempIndex]; 
        [cell changeArrowWithUp:NO]; 

       } 
       Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:self.selectIndex]; 
       [cell changeArrowWithUp:YES]; 
       [self didSelectCellRowFirstDo:NO nextDo:YES]; 
      } 
     } 

    }else 
    { 

     NSDictionary *dic = [dataList objectAtIndex:indexPath.section]; 
     NSArray *list = [dic objectForKey:@"list"]; 
     NSString *item = [list objectAtIndex:indexPath.row-1]; 
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"sample tree" message:item delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
     [alert show]; 

//  NSArray* temp=[NSArray arrayWithObjects:one,second,third,nil]; 
//  [self viewPresent:[temp objectAtIndex:indexPath.row-1] sectionAt:item]; 
    } 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
//method for pushing view controller 
-(void)viewPresent:(UIViewController *)viewName sectionAt:(NSString *)sectionValue 
{ 
    [[self navigationController]pushViewController:viewName animated:YES]; 
} 

- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert 
{ 
    self.isOpen = firstDoInsert; 


    [self.expansionTableView beginUpdates]; 

    int section = self.selectIndex.section; 
    int contentCount = [[[dataList objectAtIndex:section] objectForKey:@"list"] count]; 
    NSMutableArray* rowToInsert = [[NSMutableArray alloc] init]; 
    for (NSUInteger i = 1; i < contentCount + 1; i++) { 
     NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section]; 
     [rowToInsert addObject:indexPathToInsert]; 
    } 

    if (firstDoInsert) 
    { [self.expansionTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop]; 
    } 
    else 
    { 
     [self.expansionTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop]; 
    } 

    [rowToInsert release]; 

    [self.expansionTableView endUpdates]; 
    if (nextDoInsert) { 
     self.isOpen = YES; 
     self.selectIndex = [self.expansionTableView indexPathForSelectedRow]; 
     [self didSelectCellRowFirstDo:YES nextDo:NO]; 
    } 
// if (self.isOpen) [self.expansionTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
} 
@end 

затем создать две ячейки с подкласса UITableViewCell

первая ячейка должна быть

.h файл

#import <UIKit/UIKit.h> 

@interface Cell1 : UITableViewCell 


@property (nonatomic,retain)IBOutlet UILabel *titleLabel; 
@property (nonatomic,retain)IBOutlet UIImageView *arrowImageView; 

- (void)indication:(BOOL)up; 
@end 

.m файл

#import "Cell1.h" 

@implementation Cell1 
@synthesize titleLabel,arrowImageView; 

- (void)dealloc 
{ 
    self.titleLabel = nil; 
    self.arrowImageView = nil; 
    [super dealloc]; 
} 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

- (void)indication:(BOOL)up 
{ 

    if (up) { 

     self.arrowImageView.image = [UIImage imageNamed:@"UpAccessory.png"]; 
    }else 
    { 
     self.arrowImageView.image = [UIImage imageNamed:@"DownAccessory.png"]; 

    } 
} 

вторая ячейка должна быть

.h файл

#import <UIKit/UIKit.h> 

@interface Cell2 : UITableViewCell 
@property (nonatomic,retain)IBOutlet UILabel *titleLabel; 
@end 

.m файл

#import "Cell2.h" 

@implementation Cell2 
@synthesize titleLabel; 
- (void)dealloc 
{ 
    self.titleLabel = nil; 
    [super dealloc]; 
} 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

для учебных целей мои значения приведены ниже

(
{ 
    list =   (
        1, 
        2, 
        3 
        ); 
    name = Numbers; 
}, 
{ 
    list =   (
        a, 
        b, 
        c 
        ); 
    name = Alaphabets; 
}, 
{ 
    list =   (
        India, 
        China, 
        Australia 
        ); 
    name = Countries; 
} 
)