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

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. Map和HashMap

    通过查询JDK帮助文档,我们可以得知Map的说明.方法等 import java.util.Map; import java.util.HashMap; class Test{ public stat ...

  2. eclipse集成maven

    1.工具下载: Eclipse4.2 jee版本(这里使用最新的Eclipse版本,3.7以上版本按照以下步骤都可以) 下载地址:http://www.eclipse.org/downloads/do ...

  3. NOJ1012-进制转换

    进制转换 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 2214            测试通过 : 645  ...

  4. 老工程升级到VS2010或以上时会出现 libc.lib 解决方法

    有些网上的工程都比较老,比如用2003之类.一般会有个静态libc.lib.在新版本里已经没有这个库,被微软无情的抛弃. 编译时会出现动态库找不到: 1>LINK : fatal error L ...

  5. try-catch-finally中return语句的执行

    catch里return后还会执行finally吗??在java里,是的.但是值得注意的是,在存在try-catch-finally的方法中,return可能出现的位置有4个,在try中,在catch ...

  6. MongoDB仲裁节点的理解以及memcached,zookeeper,redis,故障恢复方案思考.

    在进行副本集部署时我们会添加一个或多个仲裁节点,仲裁节点不用于备份数据,由于它职责的职责是负责选举主节点,所以对硬件没有太高要求,可以将它部署在单独的服务器上,这个服务器可以是监听服务器,也可以部署在 ...

  7. spring aop 使用注解方式总结

    spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...

  8. [原创]PostgreSQL Plus Advanced Server监控工具PEM(一)

    一.概述 PEM是为数据库管理员.系统架构师和性能分析师为管理.监控和优化 PostgreSQL 和 EnterpriseDB 数据库服务器设计的图形化管理工具.旨在解决大量数据库服务器跨地域.精细化 ...

  9. 代码文档生成工具-Doxygen生成CHM和RTF图文教程

    Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,可以从一套归档源文件开始,生成chm格式的文档.本文主要讲解如何在winddows下安装doxygen.     1.下载do ...

  10. php之常用函数库

    1.时间和日期 如何获取时间戳 time()--从1970年开始计算的毫秒数 echo time(); 日期 echo date('Y-m-d H:i:s'); 获取默认是时区 echo date_d ...