ios 团购分类页面(9宫格)
=-= 命名有些错误,但功能实现,以后注意下命名规范
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宫格)的更多相关文章
- ios 团购信息客户端demo(一)
团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...
- ios 团购信息客户端demo(三)
接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入AQGridView中显示出来,因为我们的工程中已经将AQGridView导入了,所以我们在KKFirstViewController中直接 ...
- ios 团购信息客户端demo(二)
接上一篇,这篇我们对我们的客户端加入KissXML,MBProgressHUD,AQridView这几个库,首先我们先加入KissXML,这是XML解析库,支持Xpath,可以方便添加更改任何节点.先 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)
大致花了一个月时间,利用各种空闲时间,将这个客户端实现了,在这里主要是想记录下,设计的大体思路以及实现过程中遇到的坑...... 这个项目的github地址:https://github.com/wz ...
- iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例
一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...
- IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)
******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...
- [iOS基础控件 - 6.6.1] 展示团购数据代码
1.主控制器: // // ViewController.m // GroupPurchase // // Created by hellovoidworld on 14/12/3. // Cop ...
- [iOS基础控件 - 6.6] 展示团购数据 自定义TableViewCell
A.需求 1.头部广告 2.自定义cell:含有图片.名称.购买数量.价格 3.使用xib设计自定义cell,自定义cell继承自UITableViewCell 4.尾部“加载更多按钮”,以及其被点击 ...
随机推荐
- NOJ1103-全排列
全排列 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 1148 测试通过 : 302 ...
- MySql 用户 及权限操作
bin/msyql -h host -u user -p bin/mysql -u mysql -p 本地登录 如无密码按回车直接进入mySql bin/mysqladmin -u roo ...
- 通过FileWatcher,监听通过web上传的图片,并进行压缩
需求是这样的,通过web传输过来的图片,无论是JS上传,还是其他的上传方式,都需要生成2张缩略图,分别是用于商品列表的小图small,和用于分享的小图share.基于不同上传方式的不同需求,使用exe ...
- 转:CentOS 7 安装Nginx
一.准备工作: 1.安装必备工具: ? 1 2 3 $ yum -y install gcc gcc-c++ autoconf automake $ yum -y install zlib zli ...
- [转]Not enough free disk space on disk '/boot'
Not enough free disk space on disk '/boot' http://my.oschina.net/u/947673/blog/277224 # 解决 出现此情况是因为你 ...
- Content Template & DataTemplate 区别
转一篇很好的博客: http://www.cnblogs.com/lzhp/p/3250786.html 介绍 listbox 的 Template.ItemsPanel.ItemContainerS ...
- 利用while(code!=EOF){}来实现“无限”循环
#include <stdio.h>int main(){ char a,b,c; char t; while(scanf("%c%c%c",& ...
- Lisp使用Lambda语法
lamdba 其实就是一个匿名函数. 定义Lisp的lambda语法非常的简单,如下: (lambda ([parameter]) [experssion]) 调用lambda的语法有三种方法,如下: ...
- vs2013中把解决方案上传到SVN服务器
在VS2013中直接上传代码到SVN服务器,在这之前,必须是你的电脑已经安装了TortoiseSVN. 其次,VS2013必须安装AnkhSVN插件.然后才可以向我下面所述一样使用TortoiseSV ...
- VS2013中如何更改主题颜色(深色)和恢复默认的窗口布局
1.通常情况下,我们会根据个人爱好更改VS2013的主题颜色,一开始我喜欢白色,后来我偏爱深色. 依次选择:工具->选项->常规->主题->深色->确定,ok 2.我们在 ...