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. Letter Combinations of a Phone Number leetcode java

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  2. 领扣-5 最长回文子串 Longest Palindromic Substring MD

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

  3. 性能优化 BlockCanary 卡顿监测 MD

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

  4. JS获取当前网页内容,创建文件并下载,URL.createObjectURL和URL.revokeObjectURL

    有时候需要在前端侧对于动态生成的内容进行下载,比如页面上某一段文本信息,再比如对页面进行分享的时候,希望分享图片是页面内容的实时截图,此时,这个图片就是动态的,纯HTML显然是无法满足我们的需求的,借 ...

  5. 卡尔曼滤波算法--核心公式推导导论 - ZZ

    卡尔曼滤波算法--核心公式推导导论 10 个月前 写在最前面:这是我第一篇专栏文章,感谢知乎提供这么一个平台,让自己能和大家分享知识.本人会不定期的开始更新文章,文章的内容应该集中在汽车动力学控制,整 ...

  6. 【Scala】Java-Scala-单例模式实现方法

    Java-Scala-单例模式实现方法 scala 单例_百度搜索 scala实现单例模式-博客-云栖社区-阿里云

  7. Red Hat 配置ip地址

    red hat 的网卡配置文件位于:/etc/sysconfig/network-scripts目录下,如ifcfg-eth0,ifcfg-eth1等等,下面进行配置: 1)DEVICE=eth0 定 ...

  8. [Algorithm] Powerset Problem

    By given an array [1,2,3]: Return all possible combinations. for example: [[], [1], [2], [3], [1, 2] ...

  9. angular.element()的用法

    1.引用jQuery的前提下,和$用法基本相同:angular.element('#').html() 例如: angular.element('#test').html() angular.elem ...

  10. Windows版Mycat结合mysql安装配置+水平切分(转载)

    来源:https://segmentfault.com/a/1190000009495748 参考文档:Mycat安装与使用 环境 环境 版本 windows 10 java 1.8.0 mysql ...