uitableview做九宫格
1:创建实体
#import <Foundation/Foundation.h> @interface Shop : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@end #import <Foundation/Foundation.h> @interface Shop : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@end
2:自定义cell和button
#import <Foundation/Foundation.h> @interface Shop : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@end #import "MyCell.h"
#import "Shop.h"
#import "MyButton.h" #define kTagPrefix 10 @implementation MyCell #pragma mark 监听每一格的点击
- (void)itemClick:(MyButton *)item {
NSLog(@"点击了-%@", item.titleLabel.text);
} - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // 按钮宽度
CGFloat btnWidth = self.contentView.bounds.size.width / kColumn; for (int i = ; i<kColumn; i++) {
MyButton *btn = [[[MyButton alloc] init] autorelease];
btn.tag = kTagPrefix + i;
[btn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(btnWidth * i, , btnWidth, kCellHeight);
//btn.backgroundColor = [UIColor redColor];
[self.contentView addSubview:btn];
} } return self;
} // 假设shops里面有2个
- (void)setRowShops:(NSArray *)shops {
int count = shops.count; for (int i = ; i<kColumn; i++) {
MyButton *btn = (MyButton *)[self.contentView viewWithTag:kTagPrefix + i]; // 设置数据
if (i<count) {
btn.hidden = NO;
Shop *shop = [shops objectAtIndex:i];
// 设置背景上面的小图片
[btn setImage:[UIImage imageNamed:shop.icon] forState:UIControlStateNormal]; [btn setTitle:shop.name forState:UIControlStateNormal];
} else {
btn.hidden = YES;
}
}
}
@end
自定义button
#import "MyButton.h"
#define kImageRatio 0.6
#define kMarginRatio 0.1
#define kLabelRatio (1 - kImageRatio - 2 * kMarginRatio)
@implementation MyButton
- (id)init {
if (self = [super init]) {
// 设置文字颜色 一定要设置否则不会显示标题
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 设置文字大小
self.titleLabel.font = [UIFont systemFontOfSize:];
// 设置文字居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
// 设置图片不要拉伸,保持原来的比例
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
// 高亮显示的时候不需要调整图片的颜色
self.adjustsImageWhenHighlighted = NO;
}
return self;
}
#pragma mark 设置文字的位置
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
return CGRectMake(, contentRect.size.height * (kImageRatio + kMarginRatio), contentRect.size.width, contentRect.size.height * kLabelRatio);
}
#pragma mark 设置图片的位置
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
return CGRectMake(, contentRect.size.height * kMarginRatio, contentRect.size.width, contentRect.size.height * kImageRatio);
}
@end
Viewcontroller
#import "MJViewController.h"
#import "MyCell.h"
#import "Shop.h" @interface MJViewController ()
@property (nonatomic, retain) NSMutableArray *shops;
@end @implementation MJViewController - (void)viewDidLoad
{
[super viewDidLoad]; self.shops = [NSMutableArray array]; for (int i = ; i<=; i++) {
Shop *shop = [[[Shop alloc] init] autorelease];
shop.name = [NSString stringWithFormat:@"大衣-%i", i]; shop.icon = [NSString stringWithFormat:@"TM.bundle/tmall_icon_cat_outing_%i.png", i%+]; [self.shops addObject:shop];
} // 不需要分隔线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
} - (void)viewDidUnload {
[super viewDidUnload];
self.shops = nil;
} - (void)dealloc {
self.shops = nil;
[super dealloc];
} #pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (self.shops.count + kColumn - )/kColumn;
} #pragma mark 每当有新的Cell进入视野范围内时,就会调用这个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 先根据标识去缓存池查找Cell对象
static NSString *identifier = @"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 说明缓存池中没有可循环利用的Cell
if (cell == nil) {
// 创建Cell的时候绑定一个标识
cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
} //0 1 2 3
//4
// // 从哪个位置开始截取
int location = indexPath.row * kColumn;
// 截取的长度
int length = kColumn; if (location + length >= self.shops.count) {
length = self.shops.count - location;
} NSRange range = NSMakeRange(location, length);
NSArray *rowShops = [self.shops subarrayWithRange:range];
[cell setRowShops:rowShops]; return cell;
} #pragma mark 设置Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kCellHeight;
}
@end
uitableview做九宫格的更多相关文章
- ios总结目录
:iOS中er二维码的使用 http://www.cnblogs.com/gcb999/p/3183655.html :iOS中根据数据自动生成有规律的(UItextField和UILabel) IO ...
- 【IOS】从android角度来实现(理解)IOS的UITableView
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3403124.html 本人从在学校开始到现在上班(13年毕 ...
- Android下载更新的安装包以及九宫格界面
继上篇博客,我接下来做的是一个九宫格界面,但是对之前的Splash页面我还有要说的就是,当出现网络异常.json解析异常或者没有更新的时候,我们都必须要跳转到我们的主页面,因为Splash页面仅是展示 ...
- UITableView的简单总结与回顾
今天突发奇想的想对UItableView做一下汇总,感觉在编程中这个控件可以千变万化也是用的最多的一个了,下面就为大家简单总结下这个控件,也许还有不足,不过还是请各位不吝赐教了哈,那么我就开始了,我会 ...
- UITableView系列(1)---Apple缓存池机制
一.概述 关于UITableView的基本使用, 其实十分简单.但是做App最重要的之一就是细致,技术方面要做到细致, 必须深入了解底层, 才能做出优化让程序跑得更快.那么这一系列文章从我实际项目中获 ...
- uitableview 优化
1. http://www.cocoachina.com/ios/20150602/11968.html 最近在微博上看到一个很好的开源项目VVeboTableViewDemo,是关于如何优化UITa ...
- iOS- 用UICollectionViewController 来进行横竖屏九宫格布局
1.简单说说UICollectionViewController 我们在做九宫格布局时,可以使用UIScrollView,也可以使用UICollectionViewController. 当我们用UI ...
- iOS开发之UICollectionViewController
1.概述 UICollectionView控件主要是用来做九宫格的,类似于android中的GridView控件.其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UI ...
- 转iOS中delegate、protocol的关系
iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...
随机推荐
- C++中JSON的使用详解【转】
https://blog.csdn.net/admin_maxin/article/details/53175779 jsoncpp 主要包含三个class:Value.Reader.Writer.注 ...
- Libnids(Library Network Intrusion Detection System) .
Libnids(Library Network Intrusion Detection System)是一个网络入侵检测开发的专业编程接口.它实现了基于网络的入侵检测系统的基本框架,并提供了一些基本的 ...
- 细说Request与Request.Params
在ASP.NET编程中,有三个比较常见的来自于客户端的数据来源:QueryString, Form, Cookie .我们可以在HttpRequest中访问这三大对象,比如,可以从QueryStrin ...
- python对json的操作总结 zz
Json简介:Json,全名 JavaScript Object Notation,是一种轻量级的数据交换格式.Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式.现在也常用于h ...
- Android -- tools
工具属性 Android 有一个专用的XML命名空间,用于使工具可以记录XML文件里的信息,并且在打包程序的进行把信息剥离到不会带来运行时期和下载大小的负面影响的程度. 这个命名空间的 URI 是 h ...
- 【Kafka】Kafka-分区数-备份数-如何设置-怎么确定-怎么修改
Kafka-分区数-备份数-如何设置-怎么确定-怎么修改 kafka partition 数量 更新_百度搜索 kafka重新分配partition - - CSDN博客 如何为Kafka集群选择合适 ...
- 数据挖掘中 决策树算法实现——Bash
数据挖掘中 决策树算法实现——Bash 博客分类: 数据挖掘 决策树 bash 非递归实现 标准信息熵 数据挖掘决策树bash非递归实现标准信息熵 一.决策树简介: 关于决策树,几乎是数据挖掘分类算法 ...
- 关于 stl的内存分配的深浅拷贝
http://blog.csdn.net/u012501459/article/details/44132147
- (转)unity3D 如何提取游戏资源 (反编译)+代码反编译
原帖:http://bbs.9ria.com/thread-401140-1-1.html 首先感谢 雨松MOMO 的一篇帖子 教我们怎么提取 .ipa 中的游戏资源.教我们初步的破解unity3d资 ...
- python命令行参数传递JSON串
有点小问题,一是传递的双引号被自动删除了,但是如果用单引号,JSON解析又不认. 所以,最后的方案是,传递单引号,但程序处理时做一个替换,替换成单引号.