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. 使用C++/libCurl/Jsoncpp读取arcgis wmts 服务(restful模式)

    前言: 最近工作需要将arcgis的wmts服务接入我们的3DGis系统平台,要求用户只输入一个rest模式的wmts服务地址,系统即可自动获取并解析其元数据信息,生成wmts图层,并渲染显示.经过多 ...

  2. Libxml2函数及使用方法概述【转】

    https://blog.csdn.net/chengwenyao18/article/details/7176082 一.关于XML: 在开始研究 Libxml2 库之前,先了解一下XML的相关基础 ...

  3. [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...

  4. CSS-页面滑屏滚动原理

    现在的网站有的时候为了简洁就是很多的单页滑屏滚动介绍,主要呈现方式有两种,一种是整体的元素一直排列下去,假设有五个需要展示的全屏页面,那么高度是500%,只是展示100%,剩下的可以通过transfo ...

  5. oracle sqlplus登陆命令

    1.语法:   {<username>[/<password>][@<connect_identifier>] | / }[AS {SYSDBA | SYSOPER ...

  6. 条件随机场(CRF)原理和实现

    版权声明:作者:金良山庄,欲联系请评论博客或私信,个人主页:http://www.jinliangxu.com/,CSDN博客: http://blog.csdn.net/u012176591   目 ...

  7. tail -f 然后grep,处理缓存的问题

    学习了:http://www.quwenqing.com/read-134.html 对日志记录做多次grep过滤输出,格式如下: tail -f log | grep xxx | grep yyy ...

  8. Cognos11只需简单几步创建你的Dashboard

    一.环境 操作系统:win10 数据库   :SQLserver 2008 R2 软件版本:IBM Cognos Analytics 11.0.6 浏览器   :IE 11 二.开始创建仪表板 2.1 ...

  9. Authentication and Authorization in ASP.NET Web API

      You've created a web API, but now you want to control access to it. In this series of articles, we ...

  10. Android 八款开源 Android 游戏引擎

    原文地址 本文内容 Angle Rokon LGame AndEngine libgdx jPCT Alien3d Catcake 最近无意间看到一篇关于 Android 搜索引擎的文章,于是搜索了, ...