UITableView与打开和closures部分

我有一个UITableView与几个部分。 我想点击一个部分“closures/打开”其内容,以显示/隐藏下面的行。 因此,我可以保留一些部分打开(其行可见),其他部分closures,下一部分紧接在前一部分的头部之下。

我怎样才能实现它? 我需要inheritanceUITableView并添加一个手势识别器,并以某种方式添加一个animation的行? 但我不确定这很容易…

谢谢

  1. 使用reloadSections:withRowAnimation触发更改;
  2. 在UITableViewDataSource委托中提供更新的行数

我不能评论这个问题,因为我没有足够的声望。
根据我的理解,你想为你的表视图实现手风琴function。
为此,请检查: –
效果或animation在UItableVIew和
如何为iPhone SDK应用程序实现手风琴视图?

只需要子类sectionHeaderView并像这样定义委托方法。

 @protocol SectionHeaderViewDelegate <NSObject> @optional -(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)section; -(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)section; @end 

然后在tableViewCOntroller.h中

 @interface TableViewController : UITableViewController <SectionHeaderViewDelegate> 

并在tableViewCOntroller.m

 -(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)sectionOpened { SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:sectionOpened]; sectionInfo.open = YES; /* Create an array containing the index paths of the rows to insert: These correspond to the rows for each quotation in the current section. */ NSInteger countOfRowsToInsert = [sectionInfo.play.quotations count]; NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < countOfRowsToInsert; i++) { [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:sectionOpened]]; } /* Create an array containing the index paths of the rows to delete: These correspond to the rows for each quotation in the previously-open section, if there was one. */ NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init]; NSInteger previousOpenSectionIndex = self.openSectionIndex; if (previousOpenSectionIndex != NSNotFound) { SectionInfo *previousOpenSection = [self.sectionInfoArray objectAtIndex:previousOpenSectionIndex]; previousOpenSection.open = NO; [previousOpenSection.headerView toggleOpenWithUserAction:NO]; NSInteger countOfRowsToDelete = [previousOpenSection.play.quotations count]; for (NSInteger i = 0; i < countOfRowsToDelete; i++) { [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenSectionIndex]]; } } // Style the animation so that there's a smooth flow in either direction. UITableViewRowAnimation insertAnimation; UITableViewRowAnimation deleteAnimation; if (previousOpenSectionIndex == NSNotFound || sectionOpened < previousOpenSectionIndex) { insertAnimation = UITableViewRowAnimationTop; deleteAnimation = UITableViewRowAnimationBottom; } else { insertAnimation = UITableViewRowAnimationBottom; deleteAnimation = UITableViewRowAnimationTop; } // Apply the updates. [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation]; [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation]; [self.tableView endUpdates]; self.openSectionIndex = sectionOpened; } -(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)sectionClosed { /* Create an array of the index paths of the rows in the section that was closed, then delete those rows from the table view. */ SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:sectionClosed]; sectionInfo.open = NO; NSInteger countOfRowsToDelete = [self.tableView numberOfRowsInSection:sectionClosed]; if (countOfRowsToDelete > 0) { NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < countOfRowsToDelete; i++) { [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:sectionClosed]]; } [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop]; } self.openSectionIndex = NSNotFound; if ([selectedIndexes count]>0) { for(NSIndexPath *indexPath in selectedIndexes) { if(sectionClosed == indexPath.section) { [sectionInfo.headerView changeOnHighlighted:YES]; break; } else { [sectionInfo.headerView changeOnHighlighted:NO]; } } } else { [sectionInfo.headerView changeOnHighlighted:NO]; } } 

而进一步的细节,你可以参考这个示例项目从iOS开发的lib ..

这里是一个简单的解决scheme,你甚至可以创build一个自定义的展开/折叠视图。 这里是简单的步骤1)创build一个自定义视图添加button。 在这里输入图像说明 ///join所有sockets,并在视图类的BOOLvariables上创build

 @property (weak, nonatomic) IBOutlet UIButton *BtnAction; @property(assign, nonatomic)BOOL isOpen; 

/ /创build一个标题添加tableview,你想要的。 这里是一个简单的逻辑添加尽可能多的,你需要的。 我已经添加了哪些是在headertitlearrays我想它是dynamic的。

 NSMutableArray * headerTitle = [NSMutableArray arrayWithObjects:@"Your Order", @"Delivery Address", @"Pay By", nil]; for (NSUInteger index = 0; index<headerTitle.count; index++) { VGOrderHeader* HeaderView = [[[NSBundle mainBundle] loadNibNamed:@"VGOrderHeader" owner:self options:nil] lastObject]; HeaderView.frame = CGRectMake(0, 0, 32, 40); HeaderView.BtnAction.tag = index; if (index == 0) { HeaderView.isOpen = YES; HeaderView.lblPlus.text = [NSString stringWithFormat:@"open"]; } [HeaderView.BtnAction addTarget:self action:@selector(selectSectionToOpen:) forControlEvents:UIControlEventTouchUpInside]; [headerArray addObject:HeaderView]; } 

///这是标题点击操作。

 -(void)selectSectionToOpen:(UIButton *)sender{ for (NSUInteger Increment=0; Increment<headerArray.count; Increment++) { if (sender.tag == Increment) { DCOrderHeader* HeaderView= headerArray[Increment]; HeaderView.isOpen = !HeaderView.isOpen; } } // little animation dispatch_async(dispatch_get_main_queue(), ^{ [UIView transitionWithView:self.tableView duration:0.55f options:UIViewAnimationOptionTransitionCrossDissolve animations:^(void) { [self.tableView reloadData]; } completion:NULL]; }); } 

///最后在表视图的标题方法中分配视图并提供一个高度

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [headerArray objectAtIndex:section]; } 

//最终触摸

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return headerArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { DCOrderHeader* HeaderView = headerArray[section]; if (HeaderView.isOpen == YES) { return self.someArray.count; }else{ return 0; } }