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.尾部“加载更多按钮”,以及其被点击 ...
随机推荐
- 在mac系统安装Apache Tomcat的详细步骤[转]
对于Apache Tomcat 估计很多童鞋都会,那么今天就简单说下在mac上进行tomcat的安装: 第一步:下载Tomcat 这里Himi下载的tomcat version:7.0.27 直接 ...
- 【HTML5】炫丽的时钟效果Canvas绘图与动画基础练习
源自慕课网 效果如下: 全部代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- IOS内存管理「3」- 自动释放的基本概念和用法
- Window7上搭建symfony开发环境(PEAR)
http://blog.csdn.net/kunshan_shenbin/article/details/7162243 1. 更新PEAR 进入PHP所在目录,找到go-pear.bat并双击. 一 ...
- 条款10:令operator=返回一个*this的引用
为了编程的简洁性,有时候需要串联赋值,如:x = y = z = 15; 由于赋值采用右结合,因此上述语句被解释为:x = (y = (z = 15)); 为了实现串联赋值,复制操作符函数必须返回一个 ...
- MSSQL Transaction[事务] and Procedure[存储过程]
--事务分三种 --1.显示事务 --我们手动begin transaction ...... commit transaction/rollback transaction --上面这种写法叫做“显 ...
- 容器适配器之stack
参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...
- .net sql 防注入 httpmodule
1 新建一个类,实现IHttpModule接口 using System; using System.Collections.Generic; using System.Linq; using Sys ...
- 从一个Activity返回上一个Activity
从一个Activity返回上一个Activity 要求:保留上一个Activity的数据 方法: 第一步:从Activity1转向Activity2时,用startActivityForResult而 ...
- jQuery设计思想
jQuery设计思想 原文网址:http://jqfundamentals.com/book/ 阮一峰 翻译整理 [目录] 一.选择网页元素 二.改变结果集 三.链式操作 四.元素的操作:取值和赋值 ...