打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
第十二章:表视图常用UI范例

1.首先自定义一个UIView 用来作为SectionHeader,用一个大的button覆盖整个View

MKAccordionButton.h文件

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface MKAccordionButton : UIView  
  4.   
  5. @property (nonatomic,weak) IBOutlet UIButton *mainButton;  
  6.                         // 返回值  快名称              参数  
  7. @property (nonatomic,copy) void(^buttonTappedHandler)();  
  8.   
  9. @end  

MKAccordionButton.m文件

这里,需要注意的就是使用button的点击事件来调用块

  1. #import "MKAccordionButton.h"  
  2.   
  3. @implementation MKAccordionButton  
  4.   
  5. - (id)initWithFrame:(CGRect)frame  
  6. {  
  7.     self = [super initWithFrame:frame];  
  8.     if (self) {  
  9.         // Initialization code  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14. - (void)awakeFromNib{  
  15.     //样式设置  
  16.     self.layer.borderColor = [UIColor colorWithWhite:0.6 alpha:0.6].CGColor;  
  17.     self.layer.borderWidth = 1.0f;  
  18.     [super awakeFromNib];  
  19. }  
  20.   
  21. - (IBAction)buttonTapped:(id)sender  
  22. {  
  23.     //如果块不为nil,则调用块  
  24.     if(self.buttonTappedHandler)  
  25.         self.buttonTappedHandler();  
  26. }  
  27.   
  28. @end  


2.在TableViewController中,实现块的方法

  1. //设置Section头部的View  
  2. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  3. {  
  4.     //根据nib实例化对象  
  5.     MKAccordionButton *button = [[[UINib nibWithNibName:@"MKAccordionButton" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];  
  6.       
  7.     [button.mainButton setTitle:[[self.objects allKeys] objectAtIndex:section] forState:UIControlStateNormal];  
  8.       
  9.     //设置块的方法体  
  10.     button.buttonTappedHandler=^{  
  11.         [self openAccordionAtIndex:section];  
  12.     };  
  13.       
  14.     return button;  
  15. }  


具体tableVIew的操作


  1. //主要方法,点击的sectionTitle时候调用  
  2. - (void)openAccordionAtIndex:(int) index{  
  3.     NSMutableArray *indexPaths = [NSMutableArray array];  
  4.       
  5.     //得到当前展开的section的行数  
  6.     int sectionCount = [[self.objects objectForKey:[[self.objects allKeys] objectAtIndex:self.currentlyExpandedIndex]] count];  
  7.       
  8.     //得到所有当前展开的行的indexPath  
  9.     for (int i=0; i<sectionCount; i++) {  
  10.         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:self.currentlyExpandedIndex];  
  11.         [indexPaths addObject:indexPath];  
  12.     }  
  13.       
  14.     //重要!!!必须将其设置为一个不存在的索引,否则出错  
  15.     //具体原因不太清楚,各位大大知道的话,帮忙评论告诉我下,谢谢。  
  16.     self.currentlyExpandedIndex=-2;  
  17.       
  18.     //删除刚才展开的indexPaths,将后面的行上移  
  19.     [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];  
  20.       
  21.     //将当前展开设置为点击的索引  
  22.     self.currentlyExpandedIndex = index;  
  23.     //得当当前点击的section拥有的行数  
  24.     sectionCount = [[self.objects objectForKey:[[self.objects allKeys] objectAtIndex:self.currentlyExpandedIndex]] count];  
  25.       
  26.     [indexPaths removeAllObjects];  
  27.       
  28.     //将其加入paths中  
  29.     for (int i=0; i<sectionCount; i++) {  
  30.         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:self.currentlyExpandedIndex];  
  31.         [indexPaths addObject:indexPath];  
  32.     }  
  33.       
  34.     //动画,将这些行加入TableView中  
  35.     double delayInSeconds = 0.35;  
  36.     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));  
  37.     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){  
  38.         [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];  
  39.     });  
  40.       
  41.     [self.tableView endUpdates];  
  42. }  

3.创建tableViewCell并给予手势
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];  
  4.     if(!cell)  
  5.     {  
  6.         cell = [[UITableViewCell alloc] init];  
  7.     }  
  8.       
  9.     NSString *str = [[self.objects objectForKey:[[self.objects allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];  
  10.       
  11.     cell.textLabel.text = str;  
  12.       
  13.     //创建手势滑动 手指在单元格滑动  
  14.     UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];  
  15.     //判断收拾是否在View中  
  16.     if(![swipeGestureRight respondsToSelector:@selector(locationInView:)])  
  17.     {  
  18.         swipeGestureRight=nil;  
  19.     }else{  
  20.         swipeGestureRight.delegate = self;  
  21.         //手指个数为1  
  22.         swipeGestureRight.numberOfTouchesRequired = 1;  
  23.         //指定手指滑动方向,只能指定一个方向  
  24.         swipeGestureRight.direction = UISwipeGestureRecognizerDirectionRight;  
  25.           
  26.         [cell.contentView addGestureRecognizer:swipeGestureRight];  
  27.     }  
  28.       
  29.     return cell;  
  30. }  
手势的Selector方法

  1. - (void)handleGesture:(UISwipeGestureRecognizer *)gestureRecognizer  
  2. {  
  3.     // 这个view是手势所属的view,也就是增加手势的那个view  
  4.     UIView *cellView = [gestureRecognizer view];  
  5.       
  6.     //direction属性: 用来指明手势滑动的方向的。  
  7.     UISwipeGestureRecognizerDirection direction = gestureRecognizer.direction;  
  8.     switch (direction) {  
  9.         case UISwipeGestureRecognizerDirectionRight:  
  10.         {  
  11.             NSLog(@"direction==UISwipeGestureRecognizerDirectionRight");  
  12.             break;  
  13.         }  
  14.         case UISwipeGestureRecognizerDirectionLeft:  
  15.         {  
  16.             NSLog(@"direction==UISwipeGestureRecognizerDirectionLeft");  
  17.             break;  
  18.         }  
  19.         case UISwipeGestureRecognizerDirectionUp:  
  20.         {  
  21.             NSLog(@"direction==UISwipeGestureRecognizerDirectionUp");  
  22.             break;  
  23.         }  
  24.         case UISwipeGestureRecognizerDirectionDown:  
  25.         {  
  26.             NSLog(@"direction==UISwipeGestureRecognizerDirectionDown");  
  27.             break;  
  28.         }  
  29.         default:  
  30.             break;  
  31.     }  
  32. }  

更多手势详解:UIGestureRecognizer学习笔记
Demo下载地址:MKAccordion_Demo

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS开发的一些奇巧淫技
iOS开发之UITextField
Touch.phase
IOS UISegmentedControl
iOS-响应上下左右滑动手势
事件监听器中this指代问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服