1.首先自定义一个UIView 用来作为SectionHeader,用一个大的button覆盖整个View
MKAccordionButton.h文件
#import <UIKit/UIKit.h> @interface MKAccordionButton : UIView @property (nonatomic,weak) IBOutlet UIButton *mainButton; // 返回值 快名称 参数 @property (nonatomic,copy) void(^buttonTappedHandler)(); @end
这里,需要注意的就是使用button的点击事件来调用块
#import "MKAccordionButton.h" @implementation MKAccordionButton - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (void)awakeFromNib{ //样式设置 self.layer.borderColor = [UIColor colorWithWhite:0.6 alpha:0.6].CGColor; self.layer.borderWidth = 1.0f; [super awakeFromNib]; } - (IBAction)buttonTapped:(id)sender { //如果块不为nil,则调用块 if(self.buttonTappedHandler) self.buttonTappedHandler(); } @end
//设置Section头部的View - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { //根据nib实例化对象 MKAccordionButton *button = [[[UINib nibWithNibName:@"MKAccordionButton" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0]; [button.mainButton setTitle:[[self.objects allKeys] objectAtIndex:section] forState:UIControlStateNormal]; //设置块的方法体 button.buttonTappedHandler=^{ [self openAccordionAtIndex:section]; }; return button; }
具体tableVIew的操作
//主要方法,点击的sectionTitle时候调用 - (void)openAccordionAtIndex:(int) index{ NSMutableArray *indexPaths = [NSMutableArray array]; //得到当前展开的section的行数 int sectionCount = [[self.objects objectForKey:[[self.objects allKeys] objectAtIndex:self.currentlyExpandedIndex]] count]; //得到所有当前展开的行的indexPath for (int i=0; i<sectionCount; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:self.currentlyExpandedIndex]; [indexPaths addObject:indexPath]; } //重要!!!必须将其设置为一个不存在的索引,否则出错 //具体原因不太清楚,各位大大知道的话,帮忙评论告诉我下,谢谢。 self.currentlyExpandedIndex=-2; //删除刚才展开的indexPaths,将后面的行上移 [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop]; //将当前展开设置为点击的索引 self.currentlyExpandedIndex = index; //得当当前点击的section拥有的行数 sectionCount = [[self.objects objectForKey:[[self.objects allKeys] objectAtIndex:self.currentlyExpandedIndex]] count]; [indexPaths removeAllObjects]; //将其加入paths中 for (int i=0; i<sectionCount; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:self.currentlyExpandedIndex]; [indexPaths addObject:indexPath]; } //动画,将这些行加入TableView中 double delayInSeconds = 0.35; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; }); [self.tableView endUpdates]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if(!cell) { cell = [[UITableViewCell alloc] init]; } NSString *str = [[self.objects objectForKey:[[self.objects allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; cell.textLabel.text = str; //创建手势滑动 手指在单元格滑动 UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; //判断收拾是否在View中 if(![swipeGestureRight respondsToSelector:@selector(locationInView:)]) { swipeGestureRight=nil; }else{ swipeGestureRight.delegate = self; //手指个数为1 swipeGestureRight.numberOfTouchesRequired = 1; //指定手指滑动方向,只能指定一个方向 swipeGestureRight.direction = UISwipeGestureRecognizerDirectionRight; [cell.contentView addGestureRecognizer:swipeGestureRight]; } return cell; }手势的Selector方法
- (void)handleGesture:(UISwipeGestureRecognizer *)gestureRecognizer { // 这个view是手势所属的view,也就是增加手势的那个view UIView *cellView = [gestureRecognizer view]; //direction属性: 用来指明手势滑动的方向的。 UISwipeGestureRecognizerDirection direction = gestureRecognizer.direction; switch (direction) { case UISwipeGestureRecognizerDirectionRight: { NSLog(@"direction==UISwipeGestureRecognizerDirectionRight"); break; } case UISwipeGestureRecognizerDirectionLeft: { NSLog(@"direction==UISwipeGestureRecognizerDirectionLeft"); break; } case UISwipeGestureRecognizerDirectionUp: { NSLog(@"direction==UISwipeGestureRecognizerDirectionUp"); break; } case UISwipeGestureRecognizerDirectionDown: { NSLog(@"direction==UISwipeGestureRecognizerDirectionDown"); break; } default: break; } }