UITableView的用法实例
2015-10-11 16:04
387 查看
UI中的高级控件 之一 UITableView 现在用的最广泛的控件 下面简单介绍它的用法
1.创建tableView
- (void)createTableView
{
_tableView = [[UITableView
alloc] init];
CGSize size =
self.view.frame.size;
_tableView.frame =
CGRectMake(0,
20, size.width, size.height-20);
//设置数据源代理
_tableView.dataSource =
self;
//注册复用标识及cell类型
[_tableView
registerClass:UITableViewCell.class
forCellReuseIdentifier:@"cellId"];
[self.view
addSubview:_tableView];
}
2.代理的必须实现的方法
//返回指定的区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
_dataSource.count;
}
//返回指定的区的指定的行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static int number = 0;
//tableView通过一个队列来管理所有的cell
//目的是为了复用(节约资源,提高效率)
//若队列中有空闲的,则直接返回
//若队列中没有空闲的,则需要创建新的cell
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"cellId"];
#if 0
//若提前已经注册复用标识及cell类型,则出列时会自行判断
//队列中有空闲的直接返回;若没有则会自动创建
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
number++;
NSLog(@"number:%d", number);
}
#endif
//cell中有三个视图textLabel/detailLabel/imageView
cell.textLabel.text =
_dataSource[indexPath.row];
return cell;
}
3.创建数据源
-(void)creatDataSourse{
mArr = [[NSMutableArray
alloc]init];
_dataSourse = [[NSMutableArray
alloc]init];
NSArray *arr = [[NSBundle
mainBundle]pathsForResourcesOfType:@"plist"
inDirectory:nil];
for (NSString *plist
in arr) {
NSRange range = [plist
rangeOfString:@"/"
options:NSBackwardsSearch];
NSString *name = [plist
substringWithRange:NSMakeRange(range.location+1,
4)];
//过滤Info.plist
//NSLog(@"%@",name);
if ([name
isEqualToString:@"Info"]) {
continue;
}
// NSLog(@"%@",name);
_imageNames = [[NSMutableArray
alloc]init];
NSArray *plistInfo = [[NSArray
alloc]initWithContentsOfFile:plist];
for (NSDictionary *dic
in plistInfo) {
NSString *imageName =dic[@"imageName"];
[_imageNames
addObject:imageName];
//NSLog(@"%@",imageName);
}
//NSLog(@"%@",_imageNames);
[mArr
addObject:_imageNames];
name = [name stringByAppendingFormat:@"(%lu)",_imageNames.count];
[_dataSourse
addObject:name];
}
NSLog(@"%@",mArr);
}
4.点击tabelView实现的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TileViewController *til= [[TileViewController
alloc]initWithImageNames:mArr[indexPath.row]];
//NSLog(@"%@",mArr);
[self.navigationController
pushViewController:til animated:YES];
}
5.在图片类中创建图片
-(instancetype)initWithImageNames:(NSArray *)names{
if (self= [super
init]) {
//根据名字
创建图片 并贴到视图控制器上
_imageNames = [[NSMutableArray
alloc]initWithArray:names];
_scrollView = [[UIScrollView
alloc]init];
// NSLog(@"%@ %lu",names,names.count);
//NSLog(@"%lu",_imageNames.count);
for (NSString *str
in _imageNames) {
NSString *name = [str
substringToIndex:4];
self.title = name;
}
}
return
self;
}
6.设置滚动视图
-(void)setSrollerView{
self.automaticallyAdjustsScrollViewInsets =
NO;
CGSize size =
self.view.frame.size;
_scrollView.frame =
CGRectMake(0,
64, size.width,size.height-64);
_scrollView.bounces =
NO;
[self
setImageToScrellerView];
[self.view
addSubview:_scrollView];
}
7.加载图片内容
-(void)setImageToScrellerView{
CGSize size =
_scrollView.frame.size;
CGFloat gap =
5;
CGFloat width = (size.width-5*gap)/4;
CGFloat height = width*3/2.0;
NSInteger rows = (_imageNames.count+3)/4;
for (NSInteger i =
0; i<rows; i++) {
for (NSInteger j =
0; j<4; j++) {
//计算本次要贴的图片在数组中的下标
NSInteger index =
4*i+j;
if (index >=
_imageNames.count) {
break;
}
CGRect frame =
CGRectMake(gap+(gap+width)*j, gap+(gap+height)*i, width, height);
UIImageView *view = [[UIImageView
alloc]initWithFrame:frame];
view.tag = index;
view.image = [UIImage
imageNamed:_imageNames[index]];
//添加单击手势
view.userInteractionEnabled =
YES;
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(tagHandle:)];
[view addGestureRecognizer:tgr];
[tgr release];
[_scrollView
addSubview:view];
[view release];
}
_scrollView.contentSize =
CGSizeMake(size.width, rows*(height+gap)+gap);
}
}
8.设置单击手势方法
- (void)tagHandle:(UITapGestureRecognizer *)tgr
{
SkimViewController *svc = [[SkimViewController
alloc]
initWithImageNames:_imageNames
index:tgr.view.tag];
svc.hidesBottomBarWhenPushed =
YES;
[self.navigationController
pushViewController:svc animated:YES];
[svc release];
}
1.创建tableView
- (void)createTableView
{
_tableView = [[UITableView
alloc] init];
CGSize size =
self.view.frame.size;
_tableView.frame =
CGRectMake(0,
20, size.width, size.height-20);
//设置数据源代理
_tableView.dataSource =
self;
//注册复用标识及cell类型
[_tableView
registerClass:UITableViewCell.class
forCellReuseIdentifier:@"cellId"];
[self.view
addSubview:_tableView];
}
2.代理的必须实现的方法
//返回指定的区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
_dataSource.count;
}
//返回指定的区的指定的行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static int number = 0;
//tableView通过一个队列来管理所有的cell
//目的是为了复用(节约资源,提高效率)
//若队列中有空闲的,则直接返回
//若队列中没有空闲的,则需要创建新的cell
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"cellId"];
#if 0
//若提前已经注册复用标识及cell类型,则出列时会自行判断
//队列中有空闲的直接返回;若没有则会自动创建
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
number++;
NSLog(@"number:%d", number);
}
#endif
//cell中有三个视图textLabel/detailLabel/imageView
cell.textLabel.text =
_dataSource[indexPath.row];
return cell;
}
3.创建数据源
-(void)creatDataSourse{
mArr = [[NSMutableArray
alloc]init];
_dataSourse = [[NSMutableArray
alloc]init];
NSArray *arr = [[NSBundle
mainBundle]pathsForResourcesOfType:@"plist"
inDirectory:nil];
for (NSString *plist
in arr) {
NSRange range = [plist
rangeOfString:@"/"
options:NSBackwardsSearch];
NSString *name = [plist
substringWithRange:NSMakeRange(range.location+1,
4)];
//过滤Info.plist
//NSLog(@"%@",name);
if ([name
isEqualToString:@"Info"]) {
continue;
}
// NSLog(@"%@",name);
_imageNames = [[NSMutableArray
alloc]init];
NSArray *plistInfo = [[NSArray
alloc]initWithContentsOfFile:plist];
for (NSDictionary *dic
in plistInfo) {
NSString *imageName =dic[@"imageName"];
[_imageNames
addObject:imageName];
//NSLog(@"%@",imageName);
}
//NSLog(@"%@",_imageNames);
[mArr
addObject:_imageNames];
name = [name stringByAppendingFormat:@"(%lu)",_imageNames.count];
[_dataSourse
addObject:name];
}
NSLog(@"%@",mArr);
}
4.点击tabelView实现的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TileViewController *til= [[TileViewController
alloc]initWithImageNames:mArr[indexPath.row]];
//NSLog(@"%@",mArr);
[self.navigationController
pushViewController:til animated:YES];
}
5.在图片类中创建图片
-(instancetype)initWithImageNames:(NSArray *)names{
if (self= [super
init]) {
//根据名字
创建图片 并贴到视图控制器上
_imageNames = [[NSMutableArray
alloc]initWithArray:names];
_scrollView = [[UIScrollView
alloc]init];
// NSLog(@"%@ %lu",names,names.count);
//NSLog(@"%lu",_imageNames.count);
for (NSString *str
in _imageNames) {
NSString *name = [str
substringToIndex:4];
self.title = name;
}
}
return
self;
}
6.设置滚动视图
-(void)setSrollerView{
self.automaticallyAdjustsScrollViewInsets =
NO;
CGSize size =
self.view.frame.size;
_scrollView.frame =
CGRectMake(0,
64, size.width,size.height-64);
_scrollView.bounces =
NO;
[self
setImageToScrellerView];
[self.view
addSubview:_scrollView];
}
7.加载图片内容
-(void)setImageToScrellerView{
CGSize size =
_scrollView.frame.size;
CGFloat gap =
5;
CGFloat width = (size.width-5*gap)/4;
CGFloat height = width*3/2.0;
NSInteger rows = (_imageNames.count+3)/4;
for (NSInteger i =
0; i<rows; i++) {
for (NSInteger j =
0; j<4; j++) {
//计算本次要贴的图片在数组中的下标
NSInteger index =
4*i+j;
if (index >=
_imageNames.count) {
break;
}
CGRect frame =
CGRectMake(gap+(gap+width)*j, gap+(gap+height)*i, width, height);
UIImageView *view = [[UIImageView
alloc]initWithFrame:frame];
view.tag = index;
view.image = [UIImage
imageNamed:_imageNames[index]];
//添加单击手势
view.userInteractionEnabled =
YES;
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(tagHandle:)];
[view addGestureRecognizer:tgr];
[tgr release];
[_scrollView
addSubview:view];
[view release];
}
_scrollView.contentSize =
CGSizeMake(size.width, rows*(height+gap)+gap);
}
}
8.设置单击手势方法
- (void)tagHandle:(UITapGestureRecognizer *)tgr
{
SkimViewController *svc = [[SkimViewController
alloc]
initWithImageNames:_imageNames
index:tgr.view.tag];
svc.hidesBottomBarWhenPushed =
YES;
[self.navigationController
pushViewController:svc animated:YES];
[svc release];
}
相关文章推荐
- GUI - Web前端开发框架
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 评价ui设计作品好坏的八个标准(界面/交互设计研究)
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- flex 控件的重要属性
- 交换机升级排障实例
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- 为Yahoo! UI Extensions Grid增加内置的可编辑器
- sql2008启动代理未将对象应用到实例解决方案
- Android控件之CheckBox、RadioButton用法实例分析
- MFC中动态创建控件以及事件响应实现方法
- Cocos2d-x UI开发之文本类使用实例