分享

IOS详解TableView——实现九宫格效果

 ccccshq 2014-06-26


根据需求九宫格的效果可以有很多种。九宫格效果应用比较广泛,实现也多种多样,比如选项抽屉效果。

这里写了一个在UITableView上显示九宫格效果的Demo。


思路:在Cell上初始化自定义按钮,根据预设的每行按钮个数来决定他们在Cell上的位置。然后响应点击事件即可。整体实现不是很难,细节上注意一下即可。


搭建界面




数据,图片来自于天猫客户端的一些资源图片,然后还是以属性字典的方式读取提前设定的数据。




程序读取数据


  1. - (void)loadData  
  2. {  
  3.     static NSString * const TitleKey = @"title";  
  4.     static NSString * const ImageNameKey = @"imagename";  
  5.     static NSString * const FlagKey = @"flag";  
  6.       
  7.     NSString *path = [[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"];  
  8.     NSArray *array = [NSArray arrayWithContentsOfFile:path];  
  9.       
  10.     if (!array)  
  11.     {  
  12.         MyLog(@"文件加载失败");  
  13.     }  
  14.     _productList = [NSMutableArray arrayWithCapacity:array.count];  
  15.     [array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {  
  16.         Product *product = [[Product alloc] init];  
  17.         product.title = dict[TitleKey];  
  18.         product.imageName = dict[ImageNameKey];  
  19.         product.flag = [dict[FlagKey] integerValue];  
  20.         [_productList addObject:product];  
  21.     }];  
  22. }  

Product是一个模型类,有三个属性,其中Flag是为了区分每个商品分类设定的。

于是要进行一些准备工作,为了达到按钮是这种效果,我们需要自定义一个按钮。


  1. - (id)initWithFrame:(CGRect)frame  
  2. {  
  3.     self = [super initWithFrame:frame];  
  4.     if (self) {  
  5.         self.imageView.contentMode = UIViewContentModeScaleAspectFit;  
  6.         self.titleLabel.font = [UIFont systemFontOfSize:15.0f];  
  7.         self.titleLabel.textAlignment = NSTextAlignmentCenter;  
  8.         [self setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];  
  9.     }  
  10.     return self;  
  11. }  
  12.   
  13. - (CGRect)imageRectForContentRect:(CGRect)contentRect  
  14. {  
  15.     CGFloat imageX = 0;  
  16.     CGFloat imageY = 0;  
  17.     CGFloat imageW = contentRect.size.width;  
  18.     CGFloat imageH = contentRect.size.height * RImageHeightPercent;  
  19.       
  20.     return CGRectMake(imageX, imageY, imageW, imageH);  
  21. }  
  22.   
  23. - (CGRect)titleRectForContentRect:(CGRect)contentRect  
  24. {  
  25.     CGFloat x = 0;  
  26.     CGFloat y = contentRect.size.height * RImageHeightPercent;  
  27.     CGFloat width = contentRect.size.width;  
  28.     CGFloat height = contentRect.size.height * (1 - RImageHeightPercent);  
  29.       
  30.     return CGRectMake(x, y, width, height);  
  31. }  

其中宏为#define RImageHeightPercent 0.7 也就是上面百分之70显示图片,下面30显示标题


然后在自定义cell上初始化按钮

  1. NSInteger width = RCellWidth/RColCount;  
  2. for (NSInteger i = 0; i < RColCount; i++)  
  3. {  
  4.     ProductButton *btn = [[ProductButton alloc] init];  
  5.     btn.frame = CGRectMake(i*width + RMarginX, RMarginY, width - 2*RMarginX, RCellHeight - 2*RMarginY);  
  6.     btn.tag = RStartTag + i;  
  7.     [self.contentView addSubview:btn];  
  8. }  

设置tag是为了一会在绑定数据的时候能够定位到每一个button

  1. - (void)bindProducts:(NSArray *)productList  
  2. {  
  3.     for (NSInteger i = 0; i < RColCount; i++)  
  4.     {  
  5.         ProductButton *btn = (ProductButton *)[self.contentView viewWithTag:RStartTag + i];  
  6.         Product *product = productList[i];  
  7.         btn.tag = product.flag;  
  8.         [btn setImage:[UIImage imageNamed:product.imageName] forState:UIControlStateNormal];  
  9.         [btn setTitle:product.title forState:UIControlStateNormal];  
  10.         [btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];  
  11.     }  
  12. }  

在Cell设置的时候传过来一个参数productList来让cell获取到要绑定的若干个Product数据模型。

并且监听事件,将具体哪个按钮被点击的事件通过设置代理的方式传递给视图控制器。

协议

  1. @class ProductCell;  
  2. @protocol ProductCellDelegate <NSObject>  
  3. @optional  
  4. - (void)productCell:(ProductCell *)cell actionWithFlag:(NSInteger)flag;  
  5. @end  

监听方法

  1. - (void)buttonTapped:(ProductButton *)sender  
  2. {  
  3.     //MyLog(@"%d", sender.tag);  
  4.     [_cellDelegate productCell:self actionWithFlag:sender.tag];  
  5. }  

视图控制器实现则根据不同情况进行不同操作即可

再看下配置单元格的方法

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  4.       
  5.     NSInteger index = indexPath.row;  
  6.     NSMutableArray *list = [NSMutableArray arrayWithCapacity:RColCount];  
  7.     for (NSInteger i = 0; i < RColCount; i++)  
  8.     {  
  9.         [list addObject:_productList[index*RColCount + i]];  
  10.     }  
  11.       
  12.     cell.cellDelegate = self;  
  13.     [cell bindProducts:list];  
  14.     return cell;  
  15. }  



到这里,主要的实现方法已经完成。具体的代码可以下载Demo:

Demo源码


最后来看下效果图





以上就是本篇博客全部内容,欢迎指正和交流 转载注明出处~

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多