=-= 命名有些错误,但功能实现,以后注意下命名规范

WJViewGroup.h

#import <UIKit/UIKit.h>

@interface WJViewGroup : UIView {
NSInteger _width;
NSInteger _height;
} @property (nonatomic,assign)NSInteger widthNum; //横排多少个
@property (nonatomic,assign)NSInteger heightNum; //竖排多少个 @property (copy, nonatomic) void(^itemBlock)(NSInteger index);//按钮点击回调
@property (nonatomic,assign)BOOL isLine;
@property (nonatomic,strong)NSMutableArray *array; /**
* 添加item
*
* @param widthItem 高数量
* @param heightItem 宽数量
* @param num item数量
*/
- (void)addItem:(NSInteger)widthItem widthItem:(NSInteger)heightItem num:(NSArray *)num;
/**
* 改变item的文字
*
* @param array 文字数组
*/
- (void)item:(NSArray *)array;
@end

WJViewGroup.m

#import "WJViewGroup.h"

@implementation WJViewGroup

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_width = frame.size.width;
self.backgroundColor = [UIColor whiteColor];
_height = frame.size.height;
self.layer.borderWidth = ;
self.clipsToBounds = YES;
self.layer.cornerRadius = ;
_widthNum = ;
_heightNum = ;
_array = [[NSMutableArray alloc]init];
}
return self;
} #pragma mark - 添加item 3行--------- - (void)addItem:(NSInteger)widthItem widthItem:(NSInteger)heightItem num:(NSArray *)num{
NSInteger index = ;
NSInteger indeNume = ;
for (NSInteger i = ; i < widthItem; i ++) {
NSInteger index_Y = ;
for (NSInteger j = ; j < heightItem; j++) { if (indeNume < num.count) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(_width/_widthNum*index_Y, _height/_heightNum * index, _width/_widthNum, _height/_heightNum)];
[button setTitle:num[indeNume] forState:UIControlStateNormal];
button.tag = indeNume;
button.titleLabel.font = [UIFont systemFontOfSize:];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
[_array addObject:button];
if (_isLine && index_Y < ) {
UIView *lineVertical = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(button.frame), CGRectGetMinY(button.frame) + _height/_heightNum *0.2, 0.5, _height/_heightNum *0.6)];
lineVertical.backgroundColor = [UIColor lightGrayColor];
[self addSubview:lineVertical];
} }
indeNume ++;
index_Y++;
} index++;
}
if (_isLine) {
[self addCrossLine:];
} } - (void)item:(NSArray *)array {
NSInteger index = ;
for (UIButton *button in _array) {
if (index < array.count) {
[button setTitle:array[index] forState:UIControlStateNormal];
}
index++;
}
} #pragma mark - 添加横竖线 - (void)addCrossLine:(NSInteger)index {
if (index == ) {
for (int i = ; i < ; i ++) {
if (i > ) {
UIView *crossLine = [[UIView alloc]initWithFrame:CGRectMake(_width *0.05, _height/_heightNum*i, _width *0.9, 0.5)];
crossLine.backgroundColor = [UIColor lightGrayColor];
[self addSubview:crossLine];
} }
}else {
for (int i = ; i < ; i ++) {
if (i > ) {
UIView *crossLine = [[UIView alloc]initWithFrame:CGRectMake(, _height/_heightNum*i, _width, 0.5)];
crossLine.backgroundColor = [UIColor lightGrayColor];
[self addSubview:crossLine];
}
}
}
} - (void)addVerticalLine {
for (int i = ; i < ; i ++) {
if (i < ) {
UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(_width/_widthNum * (i+),, _width, 0.5)];
verticalLine.backgroundColor = [UIColor grayColor];
[self addSubview:verticalLine];
}
}
} #pragma mark - 事件监听 - (void)buttonPressed:(UIButton *)sender {
if (sender.titleLabel.text.length > ) {
if (_itemBlock) {
_itemBlock(sender.tag);
} }
} @end

使用:

1.直接使用

  WJViewGroup *group = [[WJViewGroup alloc]initWithFrame:CGRectMake(ScreenWidth*0.05, , ScreenWidth*0.9, )];
group.isLine = YES; group.layer.borderColor = [UIColor colorWithHexString:@"d3d1d1"].CGColor;
[group addItem: widthItem: num:@[@"美食",@"电影",@"KTV",@"龙虾",@"甜品",@"海鲜",@"火锅",@"自助餐",@"蔬菜"]];
[group setItemBlock:^(NSInteger index) {
NSLog(@"%ld",index); }];
[self.view addSubview:group];

效果图:

2.cell中使用

自定义初始化cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellHeight:(NSInteger)cellHeight; 
      _group = [[WJViewGroup alloc]initWithFrame:CGRectMake(ScreenWidth*0.05, CGRectGetHeight(viewBG.frame) +, ScreenWidth*0.9, cellHeight)];
_group.isLine = YES;
if (cellHeight == ) {
_group.heightNum = ;
}
_group.layer.borderColor = [UIColor colorWithHexString:@"d3d1d1"].CGColor;
[_group addItem: widthItem: num:@[@"",@"",@"",@"",@"",@"",@"",@"",@""]];
[self.contentView addSubview:_group];

懒加载:

- (NSArray *)arrayContext {
if (!_arrayContext) {
_arrayContext = [[NSArray alloc]initWithObjects:
@[@"火锅",@"蛋糕甜点",@"西餐",@"自助餐",@"小吃快餐",@"香锅烤鱼",@"聚餐宴请",@"烧烤烤肉",@"日韩料理"],
@[@"经济型酒店",@"豪华酒店",@"主题酒店",@"公寓型酒店",@"客栈",@"青年旅社"],
@[@"美发",@"美甲",@"美容美体",@"瑜伽/舞蹈"],
@[@"温泉",@"海洋馆",@"展览馆",@"动植物园",@"主题公园",@"水上乐园"],
@[@"演出赛事",@"桌游/电玩",@"运动健身",@"足疗按摩",@"洗浴/汗蒸",@"4D/5D电影",],
nil];
}
return _arrayContext;
}

代理方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    [cell.group item:self.arrayContext[indexPath.row]];
[cell.group setItemBlock:^(NSInteger index) {
NSLog(@"%ld",index); }];

效果图:

ios 团购分类页面(9宫格)的更多相关文章

  1. ios 团购信息客户端demo(一)

    团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...

  2. ios 团购信息客户端demo(三)

    接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入AQGridView中显示出来,因为我们的工程中已经将AQGridView导入了,所以我们在KKFirstViewController中直接 ...

  3. ios 团购信息客户端demo(二)

    接上一篇,这篇我们对我们的客户端加入KissXML,MBProgressHUD,AQridView这几个库,首先我们先加入KissXML,这是XML解析库,支持Xpath,可以方便添加更改任何节点.先 ...

  4. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  5. iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)

    大致花了一个月时间,利用各种空闲时间,将这个客户端实现了,在这里主要是想记录下,设计的大体思路以及实现过程中遇到的坑...... 这个项目的github地址:https://github.com/wz ...

  6. iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例

    一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...

  7. IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

  8. [iOS基础控件 - 6.6.1] 展示团购数据代码

      1.主控制器: // // ViewController.m // GroupPurchase // // Created by hellovoidworld on 14/12/3. // Cop ...

  9. [iOS基础控件 - 6.6] 展示团购数据 自定义TableViewCell

    A.需求 1.头部广告 2.自定义cell:含有图片.名称.购买数量.价格 3.使用xib设计自定义cell,自定义cell继承自UITableViewCell 4.尾部“加载更多按钮”,以及其被点击 ...

随机推荐

  1. RAP开发入门-开发笔记

    一.发布/运行 每次项目发布时需要在MANIFEST.MF->bulid中勾选依赖包.文件.代码等,避免报错 部署时项目可能会报一个baseline的错误,window->preferen ...

  2. oracle 配置 oem

    1.启动命令是[oracle@yoon ~]$ cd $ORACLE_HOME/bin [oracle@yoon ~]$ ./emctl start dbconsole 停止命令是[oracle@yo ...

  3. Python常用内建模块

    Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...

  4. hdu 5146 Sequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5146 Sequence Description Today we have a number sequ ...

  5. hdu 5199 Gunner

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5199 简单题,stl水之... #include<algorithm> #include& ...

  6. iOS学习之UIControl

    一.UIControl初识      1.UIControl是有控制功能的视图(比如UIButton.UISlider.UISegmentedControl等)的父类. 只要跟控制有关的控件都是继承于 ...

  7. hi3531播放1080p60f, 延迟越来越大的问题与解决办法

    问题 hi3531播放1080p60f, 延迟越来越大 左边屏幕是ffplay播放的,右边屏幕是3531播放的 数据是udp组播 mpegts, h264 12M码流 原因 经过测试发现: 解码器中缓 ...

  8. org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

    org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...

  9. Node.js 学习(六)Node.js EventEmitter

    Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一个fs.read ...

  10. Entity Framework走马观花之把握全局

    在深入学习某项技术之前,应该努力形成对此技术的总体印象,并了解其基本原理,本文的目的就在于此. 一.理解EF数据模型 EF本质上是一个ORM框架,它需要把对象映射到底层数据库中的表,为此,它使用了三个 ...