IOS中UITableview中封装九宫格
第一步引入SecondNav目录即可
第二步引入头文件
#import "DIYTableView.h"
#import "invoiceInfo.h"
实现协议
DIYButtonDelegate
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *mydata=[NSMutableArrayarray];
NSString *path=[[NSBundlemainBundle] pathForResource:@"Pad_menu_level"ofType:@"json"];
NSError * error=nil;
NSString *content=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:&error];
if (error) {
NSLog(@"读取%@错误",path);
return;
}
NSArray *ContentArrary=[content JSONValue];
for (NSDictionary *dic in ContentArrary) {
InvoiceInfo *info=[InvoiceInfoInvoice:dic];
[mydata addObject:info];
}
CGFloat h=tpRec.size.height-KBottom;
CGRectDivide(tpRec, &imgRec, &tpRec, h, CGRectMinYEdge);
CGRect navRect=UIEdgeInsetsInsetRect(imgRec, UIEdgeInsetsMake(5, 5, 5, 5));
//二级导航
DIYTableView *NavSecond=[[DIYTableViewalloc] initWithFrame:navRect delegate:self];
[self.view addSubview: NavSecond];
NavSecond.aData=mydata;
[NavSecond release];
} //实现代理方法
-(void)DiyButtonClick:(DIYButton *) btn{
NSLog(@"title-->%@",btn.titleLabel.text);
}
下载地址 http://pan.baidu.com/share/link?shareid=1824940819&uk=923776187
以下参照代码
封装model
#import <Foundation/Foundation.h> @interface InvoiceInfo : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *imagUrl;
@property(nonatomic,copy)NSString *iPad_ctrls; +(id)Invoice:(NSDictionary *)info;
@end #import "InvoiceInfo.h" @implementation InvoiceInfo
+(id)Invoice:(NSDictionary *)info{
InvoiceInfo *invoice=[[[InvoiceInfo alloc] init] autorelease];
invoice.name=info[@"name"];
invoice.imagUrl=info[@"iPad_image"];
invoice.iPad_ctrls=info[@"iPad_ctrls"];
return invoice;
} - (void)dealloc
{
[_name release];
[_imagUrl release];
[_iPad_ctrls release];
[super dealloc];
} @end
自定义button
#import <UIKit/UIKit.h>
#define KFont 15 @interface DIYButton : UIButton @property(nonatomic,copy)NSString *ctrlName;
@end #import "DIYButton.h"
#define KImageHeight 0.6
#define KPadding 0.1
#define KTitleHeight (1-KImageHeight-2*KPadding) @implementation DIYButton - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//设置文字
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.titleLabel.textAlignment=NSTextAlignmentCenter;
self.titleLabel.font=[UIFont systemFontOfSize:KFont];
//设置图片
self.imageView.contentMode=UIViewContentModeScaleAspectFit;
self.adjustsImageWhenHighlighted=NO;
}
return self;
} -(CGRect)titleRectForContentRect:(CGRect)contentRect{
int width =contentRect.size.width;
int height=contentRect.size.height;
return CGRectMake(, height*(KPadding+KImageHeight), width, height*KTitleHeight);
} -(CGRect )imageRectForContentRect:(CGRect)contentRect{
int width =contentRect.size.width;
int height=contentRect.size.height;
return CGRectMake(, height*KPadding, width, height*KImageHeight); } - (void)dealloc
{
[_ctrlName release];
[super dealloc];
} @en
自定义cell
#import <UIKit/UIKit.h>
#import "InvoiceInfo.h"
#import "DIYButton.h"
#define KCount 8
#define KHeight 100
#define KMinTag 10 @protocol DIYButtonDelegate; @interface DIYCell : UITableViewCell
@property(nonatomic,retain)NSArray *data;
@property(nonatomic,assign)id<DIYButtonDelegate> diyDelegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; @end @protocol DIYButtonDelegate <NSObject> -(void)DiyButtonClick:(DIYButton *) btn; @end #import "DIYCell.h" #define KPadding 25 @implementation DIYCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
for (int i=; i<KCount; i++) {
DIYButton *btn=[[DIYButton alloc] initWithFrame:CGRectZero];
btn.tag=KMinTag+i;
[self.contentView addSubview:btn];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
}
return self;
} -(void)click:(DIYButton *)btn{
if (self.diyDelegate &&[self.diyDelegate respondsToSelector:@selector(DiyButtonClick:)]) {
[self.diyDelegate DiyButtonClick:btn];
}
} -(void)setData:(NSArray *)data{
if (_data!=data) {
[_data release];
_data=[data retain];
int count =self.data.count;
for (int i=; i<KCount; i++) {
DIYButton *btn=(DIYButton *)[self.contentView viewWithTag:(i+KMinTag)];
if (i<count) {
btn.hidden=NO;
InvoiceInfo *sp=data[i];
[btn setTitle:sp.name forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:sp.imagUrl] forState:UIControlStateNormal];
btn.ctrlName=sp.iPad_ctrls;
}
else{
btn.hidden=YES;
}
}
}
} -(void)layoutSubviews{
[super layoutSubviews]; int width=self.contentView.bounds.size.width/KCount;
CGFloat height=self.contentView.bounds.size.height;
//button事件宽度
CGFloat btnWidth=width-KPadding;
CGFloat btnheight=height-KPadding;
CGFloat centWidth=width*0.5f;
for (UIView *child in self.contentView.subviews) {
if ([child isKindOfClass:[UIButton class]]) {
int tag=child.tag-KMinTag;
if (tag>= &&tag<KCount) {
child.bounds=CGRectMake(, , btnWidth, btnheight); child.center=CGPointMake(tag*width+ centWidth, height*0.5f);//这一步很关键, 先平分cell的宽度,让后button在剧中 }
}
}
} @end
封装tableview
#import <UIKit/UIKit.h>
#import "DIYCell.h" @interface DIYTableView : UIView<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,readonly)UITableView *tableview;
@property(nonatomic,retain)NSMutableArray *aData;
@property(nonatomic,assign)id tableDelegate;
- (id)initWithFrame:(CGRect)frame delegate:(id)adelegate;
@end #import "DIYTableView.h"
#import "DIYCell.h" @implementation DIYTableView - (id)initWithFrame:(CGRect)frame delegate:(id)adelegate;
{
self = [super initWithFrame:frame];
if (self) {
_tableview=[[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
[self addSubview:_tableview];
_tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
_tableview.backgroundColor=[UIColor clearColor]; _tableview.delegate=self;
_tableview.dataSource=self;
self.tableDelegate=adelegate;
[_tableview release];
}
return self;
} #pragma mark -tableview data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int count=self.aData.count/KCount;
if (!self.aData.count%KCount==) {
count++;
}
return count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentiry=@"DIYTableView";
DIYCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentiry];
if (cell==nil) {
cell=[[[DIYCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentiry] autorelease];
}
cell.diyDelegate=self.tableDelegate; // 设置cell的背景色
UIView *bg = [[[UIView alloc] init] autorelease];
bg.backgroundColor=[UIColor whiteColor];
cell.backgroundView = bg; // 选中的背景
UIView *selectdBg = [[[UIView alloc] init] autorelease];
selectdBg.backgroundColor = [UIColor whiteColor];
cell.selectedBackgroundView = selectdBg; //起始数据
int location=indexPath.row*KCount;
int length=KCount;
if (location+length>self.aData.count) {
length=self.aData.count-location;
}
NSArray *temparr=[self.aData subarrayWithRange:NSMakeRange(location, length)];
cell.data=temparr;
return cell;
} #pragma mark -tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ;
} @end
viewcontroller用法 必须实现
DIYButtonDelegate协议
NSMutableArray *mydata=[NSMutableArray array];
NSString *path=[[NSBundle mainBundle] pathForResource:@"Pad_menu_level" ofType:@"json"];
NSError * error=nil;
NSString *content=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"读取%@错误",path);
return;
} NSArray *ContentArrary=[content JSONValue];
for (NSDictionary *dic in ContentArrary) {
InvoiceInfo *info=[InvoiceInfo Invoice:dic];
[mydata addObject:info];
}
CGFloat h=tpRec.size.height-KBottom;
CGRectDivide(tpRec, &imgRec, &tpRec, h, CGRectMinYEdge); CGRect navRect=UIEdgeInsetsInsetRect(imgRec, UIEdgeInsetsMake(, , , ));
//二级导航
DIYTableView *NavSecond=[[DIYTableView alloc] initWithFrame:navRect delegate:self];
[self.view addSubview: NavSecond];
NavSecond.aData=mydata;
[NavSecond release];
-(void)DiyButtonClick:(DIYButton *) btn{
NSLog(@"第二导航__%@__%@",btn.titleLabel.text,btn.ctrlName);
[selfpushNavVc:btn.ctrlName];
}
IOS中UITableview中封装九宫格的更多相关文章
- iOS之UITableView中的cell因为重用机制导致新的cell的数据出现重复或者错乱
UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚 ...
- iOS解决UITableView中Cell重用带来的问题
tableView的常规配置,当超出一屏的cell就会标上可重用的标识出列到可重用缓存池中,后面再根据可重用标识来到的可重的cell就会和前面显示同样内容. - (UITableViewCell *) ...
- ios 自定义UITableView中分组的标题sectionview
//Section的标题栏高度 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)sec ...
- ios 更改UITableview中Section的字体颜色
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UILabel *c ...
- iOS设置UITableView中Cell被默认选中后怎么触发didselect事件
//默认选中某个cell [self.searchResultTV selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] a ...
- ios中封装九宫格的使用(二级导航)
效果图 一般用于导航功能 第一步下载http://pan.baidu.com/share/link?shareid=1824940819&uk=923776187 第二步 把下图内容放在你的x ...
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)
iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath 首先分析有几种原因,以及相应的解决方法 1.UITableViewCell的userInterac ...
- IOS开发中UITableView(表视图)的滚动优化及自定义Cell
IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...
- ios UITableView中Cell重用机制导致内容重复解决方法
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
随机推荐
- 构建-0 Gradle DSL 属性和方法【API】
Android Plugin DSL Reference This is the DSL reference for Android Gradle Plugin. Start reading by f ...
- Hibernate缓存应用的积累与总结
Hibernate缓存一直比较难掌握,下面就分析和总结原因,相信你就会慢慢清楚了原来Hibernate缓存也是可以轻松掌握的,但前提要求大家必须跟着动手去验证一下,再用心体会,光看是没有用的 目录: ...
- Java面试问题总结
前几天Java面试中遇到的问题,这仅仅是当中的一部分问题.面试中有非常多问题是关于数据结构和算法的.在这里做下总结,希望有能力的人能够试着做一下,并在评论区留下您的答案.让大家相互学习.谢谢 程序设计 ...
- SQL-根据多个条件更新数据
根据多个条件更新数据 UPDATE sphwph SET BKXSHL=t2.BKXSHL FROM sphwph t1,sphwph_170420 t2 --(SELECT a.* FROM dbo ...
- 主机无法访问虚拟机的apache解决办法
1.前言 今天学习搭建wordpress,apache服务器安装在虚拟机的Centos上.配置好以后,发现在虚拟机上可以访问,但在windows主机上不能访问.于是百度.google一下,终于解决问题 ...
- wifidog 源码初分析(1)-转
wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码. 在路由器上启动 wifidog 之后,wifid ...
- (转)思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用问题:左乘/右乘,行优先/列优先,...
转自:http://www.cnblogs.com/soroman/archive/2008/03/21/1115571.html 思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用1. ...
- [Git] Move some commits to a separate branch that I have accidentally committed to master
Gosh no, I just added all of these commits to master. They were thought to be peer reviewed first in ...
- 编程王道,唯“慢”不破
原文地址 人和人之间编程速度的差异还是很大的,有的程序猿写代码非常快,有的却常常是龟速.Jeffrey Ventrella 最近在一篇文章里探讨了这种编程速度的差异,他是绝对的龟速派代表,来看看他对编 ...
- linux系统安装apache服务器
命令行下安装: sudo apt-get install apache2 安装完毕以后, 打开127.0.0.1,可以看到首页: 静态页面的路径是: /var/www/html 作者: NONO 出处 ...