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做九宫格的更多相关文章

  1. ios总结目录

    :iOS中er二维码的使用 http://www.cnblogs.com/gcb999/p/3183655.html :iOS中根据数据自动生成有规律的(UItextField和UILabel) IO ...

  2. 【IOS】从android角度来实现(理解)IOS的UITableView

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3403124.html   本人从在学校开始到现在上班(13年毕 ...

  3. Android下载更新的安装包以及九宫格界面

    继上篇博客,我接下来做的是一个九宫格界面,但是对之前的Splash页面我还有要说的就是,当出现网络异常.json解析异常或者没有更新的时候,我们都必须要跳转到我们的主页面,因为Splash页面仅是展示 ...

  4. UITableView的简单总结与回顾

    今天突发奇想的想对UItableView做一下汇总,感觉在编程中这个控件可以千变万化也是用的最多的一个了,下面就为大家简单总结下这个控件,也许还有不足,不过还是请各位不吝赐教了哈,那么我就开始了,我会 ...

  5. UITableView系列(1)---Apple缓存池机制

    一.概述 关于UITableView的基本使用, 其实十分简单.但是做App最重要的之一就是细致,技术方面要做到细致, 必须深入了解底层, 才能做出优化让程序跑得更快.那么这一系列文章从我实际项目中获 ...

  6. uitableview 优化

    1. http://www.cocoachina.com/ios/20150602/11968.html 最近在微博上看到一个很好的开源项目VVeboTableViewDemo,是关于如何优化UITa ...

  7. iOS- 用UICollectionViewController 来进行横竖屏九宫格布局

    1.简单说说UICollectionViewController 我们在做九宫格布局时,可以使用UIScrollView,也可以使用UICollectionViewController. 当我们用UI ...

  8. iOS开发之UICollectionViewController

    1.概述 UICollectionView控件主要是用来做九宫格的,类似于android中的GridView控件.其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UI ...

  9. 转iOS中delegate、protocol的关系

    iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...

随机推荐

  1. QMUI UI库 控件 弹窗 列表 工具类 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. 我对android davilk 虚拟机的理解

    Davilk虚拟机作为Android平台的一部分.Google公司花了大量时间思考针对低功耗手持设备的优化设计.在智能手机出现之前,与桌面设备相比,手持设备在内存和速度方面落后8-10年.它们的计算能 ...

  3. grunt-cmd-transport提取deps[]的BUG

    该BUG已经在GitHub上提了issue,详见:#56 文件 // employee/static/adder.js define(function (require, exports) { exp ...

  4. unity3d shader 学习

    [浅墨Unity3D Shader编程] 着色器参考 [Unity Shaders]

  5. ScrollView嵌套ListView,ListView完全展开及makeMeasureSpec测量机制原理分析

    在实际应用中,经常会碰到非常规的布局要求,比如说在ScrollView里嵌套ListView,ScrollView和ListView都是可以滚动的控件,这样布局看似很奇怪,但是有些效果又不得不这样做. ...

  6. SuperMap开发入门4——保存的坑

    工作空间.地图等的打开.保存.删除是最基本的操作,可我居然卡在保存工作空间的问题上了. 功能需求 需求很简单:打开工作空间中的某一地图,缩放一下,保存地图.(下次打开时,已经缩放到上次修改过的地方) ...

  7. WinForm 之 VS2010发布、打包安装程序

    第一步.在vs2010 打开要打包的应用程序解决方案,右键“ 解决方案 ” → “ 添加 ” → “ 新建项目 ” → “ 其他项目类型 ” → “ 安装和部署 ” → “ Visual Studio ...

  8. poj 3042 Grazing on the Run

    这个题目原型应该是吃完所有的草丛的最小时间,现在变成了每个草丛被吃的时间和,貌似如果还是按照原来的dp方法dp[i][j]表示吃完i到j的草丛的花掉的时间的话,有两个因素会影响后面的决策,一个是花掉的 ...

  9. 转:nginx基础概念(keepalive、pipe)

    keapalive 当然,在nginx中,对于http1.0与http1.1也是支持长连接的.什么是长连接呢?我们知道,http请求是基于TCP协议之上的,那么,当客户端在发起请求前,需要先与服务端建 ...

  10. 算法笔记_204:第四届蓝桥杯软件类决赛真题(Java语言C组)

    目录 1 好好学习 2 埃及分数 3 金蝉素数 4 横向打印二叉树 5 危险系数 6 公式求值   前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 好好学习 汤姆跟爷爷来中国旅游.一天,他帮助中国的 ...