第一步引入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(0, height*(KPadding+KImageHeight), width, height*KTitleHeight);
}-(CGRect )imageRectForContentRect:(CGRect)contentRect{int width =contentRect.size.width;int height=contentRect.size.height;return CGRectMake(0, 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=0; 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=0; 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>=0 &&tag<KCount) {child.bounds=CGRectMake(0, 0, 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 1;
}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{int count=self.aData.count/KCount;if (!self.aData.count%KCount==0) {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 100;
}@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(5, 5, 5, 5));//二级导航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中封装九宫格相关推荐

  1. iOS开发UI篇-在UItableview中实现加载更多功能

    iOS开发UI篇-在UItableview中实现加载更多功能 一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时 ...

  2. iOS开发之UITableView中计时器的几种实现方式(NSTimer、DispatchSource、CADisplayLink)

    最近工作比较忙,但是还是出来更新博客了.今天博客中所涉及的内容并不复杂,都是一些平时常见的一些问题,通过这篇博客算是对UITableView中使用定时器的几种方式进行总结.本篇博客会给出在TableV ...

  3. iOS网络开发中的同步、异步和请求队列

    在iOS网络编程中,我们经常会遇到线程的同步和异步问题,同时为了对异步请求更加精准丰富的控制,我们还常常在iOS中使用请求队列,下面就来谈谈iOS开发中同步.异步以及请求队列的使用方法. 1. 同步意 ...

  4. iOS开发 Xcode8中遇到的问题及改动

    2019独角兽企业重金招聘Python工程师标准>>> iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1. ...

  5. iOS开发——基础篇——iOS开发 Xcode8中遇到的问题及改动

    iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康都会 ...

  6. 在UITableView中使用自动布局以获取动态单元格布局和可变的行高

    本文翻译自:Using Auto Layout in UITableView for dynamic cell layouts & variable row heights 如何在表格视图的U ...

  7. 动态改变UITableView中的Cell高度

    往往在开发iPhone的应用过程中用得最多的应该算是UITableVIew了,凭着IOS给UITableView赋予了这种灵活的框架结构,让它不管在显示列表方面还是在排版方面都有着一定的优势.虽然UI ...

  8. ios去掉字符串中的某个字符_iOS 截取字符串中两个指定字符串中间的字符串方法...

    例如,要截取一个字符串中,两个指定字符串中间的字符串,OC截取方法如下: // 要截取 "> 和 之间的汉字内容: @implementation ViewController - ( ...

  9. iOS标准库中常用数据结构和算法之内存池

    上一篇:iOS标准库中常用数据结构和算法之位串 ⛲️内存池 内存池提供了内存的复用和持久的存储功能.设想一个场景,当你分配了一块大内存并且填写了内容,但是你又不是经常去访问这块内存.这样的内存利用率将 ...

最新文章

  1. python单例模式基于__new__方法实现的单例模式(推荐使用,方便)
  2. 计算机设计目的和意义,程序设计的目的和意义.doc
  3. Struts2框架--学习笔记(下):OGNL表达式、值栈操作、拦截器、struts2标签、文件上传
  4. poj2823 Sliding Window
  5. 如何确保SAP OData服务的返回结构为JSON格式
  6. 三十好几的程序员被领导责骂,只能到厕所痛哭!
  7. 执行pip命令时遇到 Fatal error in launcher: Unable to create process using ''
  8. 2020-12-28 learning opencv3: 十:Smoothing
  9. elisa数据处理过程图解_ELISA操作流程
  10. html如实现留言板功能,JavaScript实现网页留言板功能
  11. 概率论与数理统计(第四版) 第二章:随机变量及其分布(第一节和第二节笔记)
  12. Ubuntu Android开发环境配置
  13. mysql当查询条件为空时不作为条件查询
  14. jsonp跨域请求原理
  15. word 2016 页码从任意页开始
  16. 位置不可用无法访问介质受写入保护的恢复方法
  17. MMCODE 1003: 小谭变美日记(c++)
  18. 关于sklearn下class_weight参数
  19. 苹果 macOS「搜狗拼音输入法」自定义双拼方案的教程
  20. 恭贺德林教点穴网成立

热门文章

  1. 解决linux中Kipmi0进程对CPU使用率很高问题
  2. Android 广播的生命周期
  3. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)
  4. if __name__ == __main__
  5. Spring MVC Controller 要点
  6. 每天一个linux命令(25):linux文件属性详解
  7. nginx0.8 + php-5.3.4 + memcached
  8. web开发工程师出路
  9. 编译linux内核报错‘make menuconfig‘ requires the ncurses libraries
  10. jlink的SWD与JTAG下载模式的对应接线方法