@interface ZZExtendViewController ()<UITableViewDataSource, UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *myTableView; @property (strong, nonatomic) NSMutableArray *dataArray; @property (assign, nonatomic) BOOL isExtend;//是否展開 @property (strong, nonatomic) NSIndexPath *seleIndex;//記錄選中 @end @implementation ZZExtendViewController - (void)viewDidLoad { [super viewDidLoad]; [self initWithUI]; // Do any additional setup after loading the view from its nib. } - (void)initWithUI { [self initWithTableView]; _isExtend = NO; //讀取文件 NSString *path = [[NSBundle mainBundle] pathForResource:@"TestData" ofType:@"plist"]; _dataArray = [[NSMutableArray alloc] initWithContentsOfFile:path]; } - (void)initWithTableView { self.myTableView.dataSource = self; self.myTableView.delegate = self; [self.myTableView registerNib:[UINib nibWithNibName:[ZZSelectedTableViewCell identifier] bundle:nil] forCellReuseIdentifier:[ZZSelectedTableViewCell identifier]]; [self.myTableView registerNib:[UINib nibWithNibName:[ZZNormalTableViewCell identifier] bundle:nil] forCellReuseIdentifier:[ZZNormalTableViewCell identifier]]; self.myTableView.tableFooterView = [UIView new]; self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone; } #pragma mark UITableViewDelegate, UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _dataArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (_isExtend) { if (self.seleIndex.section == section) { return [[[_dataArray objectAtIndex:section] objectForKey:@"list"] count]+1; } } return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.isExtend && self.seleIndex.section == indexPath.section&&indexPath.row!=0) { //點擊展開的cell ZZNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[ ZZNormalTableViewCell identifier]]; NSArray *lists = [[_dataArray objectAtIndex:self.seleIndex.section] objectForKey:@"list"]; cell.titleLabel.text = lists[indexPath.row-1]; return cell; }else { //正常的cell ZZSelectedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[ZZSelectedTableViewCell identifier]]; NSString *name = [[_dataArray objectAtIndex:indexPath.section] objectForKey:@"name"]; cell.titleLabel.text = name; // [cell changeArrowWithUp:([self.seleIndex isEqual:indexPath]?YES:NO)]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { if ([indexPath isEqual:self.seleIndex]) { self.isExtend = NO; [self didSelectCellRowFirstDo:NO nextDo:NO]; self.seleIndex = nil; }else { if (!self.seleIndex) { //第一次點擊而且點擊section self.seleIndex = indexPath; [self didSelectCellRowFirstDo:YES nextDo:NO]; }else { //不是第一次點擊 [self didSelectCellRowFirstDo:NO nextDo:YES]; } } } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } - (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert { //firstDoInsert當前點擊的section self.isExtend = firstDoInsert; ZZSelectedTableViewCell *cell = (ZZSelectedTableViewCell *)[self.myTableView cellForRowAtIndexPath:self.seleIndex]; [cell changeArrowWithUp:firstDoInsert]; [self.myTableView beginUpdates]; NSInteger section = self.seleIndex.section; NSInteger contentCount = [[[_dataArray 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.myTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationNone]; } else { [self.myTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationNone]; } [self.myTableView endUpdates]; if (nextDoInsert) { self.isExtend = YES; self.seleIndex = [self.myTableView indexPathForSelectedRow]; [self didSelectCellRowFirstDo:YES nextDo:NO]; } if (self.isExtend) { [self.myTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end