您的位置:首页 > 产品设计 > UI/UE

UI控件笔记(十一):UI之UITableView\自定义UITableView\分组

2016-05-17 10:25 429 查看
一、UITableView表格显示

注意:用table要准备一个数组源数组,数组里可能保存的是字典、模型

#import "MainViewController.h"

1、声明协议

//dataSource和显示的内容相关的协议

//delegate和交互相关的协议

@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataArr;

@end

@implementation MainViewController

-(void)dealloc

{

self.dataArr =nil;

[super dealloc];

}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if(self)

{

//禁止用类方法给assign的属性或者成员变量赋值,自动+1-1.

self.dataArr = [NSMutableArrayarrayWithCapacity:0];

}

return
self;

}

- (void)viewDidLoad {

[superviewDidLoad];

self.automaticallyAdjustsScrollViewInsets =NO;

[self loadData];

[self makeUI];

}

2、准备数据(网络请求)

-(void)loadData

{

//做点假数据

for(int i =0;i<20;i++)

{

[self.dataArraddObject:@{@"pic":@"0.png",@"title":[NSStringstringWithFormat:@"第%d行",i]}];

}

}

3、制作UI

-(void)makeUI

{

UITableView *table = [[UITableViewalloc]
initWithFrame:CGRectMake(0,64,
320,
self.view.frame.size.height-64)style:UITableViewStylePlain];

table.dataSource =
self;

table.delegate =
self;

[self.viewaddSubview:table];

[table release];

//table的两行cell之间分界线那点事儿

table.separatorStyle =UITableViewCellSeparatorStyleNone;

}

4、UITableView遵守的协议

#pragma mark table的代理方法们

4.1、有多少个段

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 3;

}

4.2、必须一,设置table每一段有多少行,第二个参数就是段,可以根据第二个参数来使得不同的段的行数是不同的

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return
self.dataArr.count;

}

4.3、必须二,设置table每一行上面显示的内容,这个方法会被多次调用,有多少行就调用多少次

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

4.3.1、确定当前所在的段

int section = indexPath.section;

//NSLog(@"现在是第%d段",section);

4.3.2、确定当前所在的行

int row = indexPath.row;

//NSLog(@"现在是第%d行",row);

4.3.3、table的cell使用了复用的概念

1、我们只实例化页面能同时显示的cell数量+1的cell

2、当一个cell从页面消失的时候,这个cell会被放进复用池

3、当一个cell将要出现在页面的时候,首先回去复用池里找有没有之前实例化过的cell,如果有,就不再实例化新的,用旧的cell改变内容即可。如果没有,就实例化一个新的cell

注意:UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease];这种写法以后不要出现了,因为这样的话每行都要实例化一个新的cell,不利于内存管理

4.3.3.1、准备一个复用标示字符串,这个标示用来以后从池子里找的时候用

staticNSString *iden =@"iden";//字符串内容无所谓,关键是现在写什么,一会儿就用什么去找

4.3.3.2、用标示字符串把table和cell做好复用池关联,如果池子里有,那么cell就是一个从池子里取出来的cell对象了,如果池子里没有,那么cell屁也不是

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:iden];

4.3.3.3、判断cell是否存在

if(cell ==nil)//判断是否取到了,nil就是没取到

{

//实例化一个cell

cell = [[[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:iden]
autorelease];

//cell自带的label。default只有一个标题,textLabel,subTitle有两个标题,一个大标题,一个小标题

NSLog(@"pppppp");

}

NSLog(@"%@",cell);

//cell父类是UIView

//cell有几个系统默认的布局属性

//self.dataArr[row]是个字典

4.3.3.4、cell的文字属性

cell.textLabel.text =self.dataArr[row][@"title"];

4.3.3.5、每行cell的左边图片属性

cell.imageView.image = [UIImageimageNamed:self.dataArr[row][@"pic"]];

注意!!!!注意!!!!注意!!!!注意!!!!

在table的cell里,不能用UIImageView *imageView来做一个UIImageView的对象,因为cell有自己默认的imageView的属性

注意!!!!注意!!!!注意!!!!注意!!!!

4.3.3.6、cell的选中效果

cell.selectionStyle =UITableViewCellSelectionStyleNone;

return cell;

}

4.4、每行的高度,每行都可以不一样高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

//根据indexPath.row来设置每一行的高度

if(indexPath.row ==0)

{

return 100;

}

else

{

return 30;

}

}

4.5、用一个文字的形式来设置段头,设置的内容会显示在段头上

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return @"pp";

}

4.6、用一个UIView或者他的子类孙子类来设置段头,随意,多少都行,如果写了这个代理,上面那个字符串的段头就被覆盖了

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

btn.frame = CGRectMake(0,0,
320, 60);

btn.backgroundColor = [UIColorgreenColor];

[btn setTitle:@"ppppppppppp"forState:UIControlStateNormal];

return btn;

}

4.7、段头高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return 60;

}

4.8、点击选中事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

//indexPath.row可以知道当前被点击是哪一行,也可以知道被点击的是哪一段

NSLog(@"被点的是%d段%d行",indexPath.section,indexPath.row);

}

4.9、取消选中事件(反选)

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"%d段%d行被取消选中了",indexPath.section,indexPath.row);

}

二、自定义UITableView表格显示

#import "MainViewController.h"

@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataArr;

@end

@implementation MainViewController

-(void)dealloc

{

self.dataArr =nil;

[super dealloc];

}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if(self)

{

self.dataArr = [NSMutableArrayarrayWithCapacity:0];

}

return
self;

}

- (void)viewDidLoad {

[superviewDidLoad];

self.automaticallyAdjustsScrollViewInsets =NO;

[self loadData];

[self makeUI];

}

-(void)loadData

{

//要准备一个10行,每行左侧一个图片(用色块代替),中间一个标题,一个价格,一个说明,一个时间,右侧一个按钮

for(int i =0;i<10;i++)

{

[self.dataArraddObject:@{@"pic":[UIColorcolorWithRed:arc4random()%255/255.0green:arc4random()%255/255.0blue:arc4random()%255/255.0alpha:1],@"title":[NSStringstringWithFormat:@"第%d个应用",i],@"price":[NSStringstringWithFormat:@"%d",arc4random()%1000],@"time":@"2014-12-2",@"info":@"这是一个不错的应用,快下吧,福利多多,谁下谁知道,你懂得!"}];

}

}

-(void)makeUI

{

UITableView *table = [[UITableViewalloc]
initWithFrame:CGRectMake(0,64,
self.view.frame.size.width,self.view.frame.size.height-64)style:UITableViewStylePlain];

table.dataSource =
self;

table.delegate =
self;

[self.viewaddSubview:table];

[table release];

}

#pragma mark table代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return
self.dataArr.count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *iden =@"pp";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:iden];

if(cell == nil)

{

cell = [[[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:iden]
autorelease];

}

//清空cell.contentView上之前的子视图

for(id tempin cell.contentView.subviews)

{

[temp removeFromSuperview];

}

//在这里对每行的cell进行布局和赋值

NSDictionary *dic =
self.dataArr[indexPath.row];//找到这一行的数据

UIView *leftImage = [[UIViewalloc]
initWithFrame:CGRectMake(10,10,
80, 80)];

leftImage.backgroundColor = dic[@"pic"];

[cell.contentViewaddSubview:leftImage];//注意cell上面的UI应该添加到cell.contentView上

[leftImage release];

UILabel *title = [[UILabelalloc]
initWithFrame:CGRectMake(110,10,
200, 25)];

title.text = dic[@"title"];

title.font = [UIFontboldSystemFontOfSize:18];

[cell.contentView
addSubview:title];

[title release];

UILabel *price = [[UILabelalloc]
initWithFrame:CGRectMake(110,35,
160, 15)];

price.text = [NSStringstringWithFormat:@"价格:%@",dic[@"price"]];

price.font = [UIFontsystemFontOfSize:13];

[cell.contentView
addSubview:price];

[price release];

UILabel *time = [[UILabelalloc]
initWithFrame:CGRectMake(110,50,
160, 15)];

time.text = [NSStringstringWithFormat:@"时间:%@",dic[@"time"]];

time.font = [UIFontsystemFontOfSize:13];

[cell.contentView
addSubview:time];

[time release];

UILabel *info = [[UILabelalloc]
initWithFrame:CGRectMake(110,65,
160, 35)];

info.text = dic[@"info"];

info.numberOfLines =
0;

info.font = [UIFontsystemFontOfSize:13];

[cell.contentView
addSubview:info];

[info release];

UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

btn.frame = CGRectMake(270,30,
40, 40);

[btn setTitle:@"down"forState:UIControlStateNormal];

[cell.contentView
addSubview:btn];

return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 100;

}

三、分组实现(qq为例)

#import "MainViewController.h"

@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataArr;

@property(nonatomic,retain)NSMutableArray *statusArr;

@end

@implementation MainViewController

-(void)dealloc

{

self.statusArr =nil;

self.dataArr =nil;

[super dealloc];

}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if(self)

{

self.dataArr = [NSMutableArrayarrayWithCapacity:0];

self.statusArr = [NSMutableArrayarrayWithCapacity:0];

}

return
self;

}

- (void)viewDidLoad {

[superviewDidLoad];

self.automaticallyAdjustsScrollViewInsets =NO;

[self loadData];

[self makeUI];

}

-(void)loadData

{

//我有四个好友分组,每个好友分组都有几个好友

[self.dataArraddObject:@[@"张三",@"李四"]];

[self.dataArraddObject:@[@"狗剩儿",@"枣花",@"铜锁"]];

[self.dataArraddObject:@[@"天才",@"二货",@"pp",@"qq"]];

[self.dataArraddObject:@[@"樱木",@"赤木",@"木暮",@"三木"]];

//给状态数组初始化数据,0表示不显示,1表示显示

for(int i =0;i<4;i++)

{

[self.statusArraddObject:@"0"];

}//现在四段都是0,所以四段都不显示

}

-(void)makeUI

{

UITableView *table = [[UITableViewalloc]
initWithFrame:CGRectMake(0,64,
320,
self.view.frame.size.height-64)style:UITableViewStylePlain];

table.dataSource =
self;

table.delegate =
self;

table.tag = 6000;

[self.viewaddSubview:table];

[table release];

}

#pragma marktable代理

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

returnself.dataArr.count;//返回段数,用大数组的长度

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

//先判断状态数组该段是否为1

if([self.statusArr[section]isEqualToString:@"1"])

{//当状态数组中该段为1时,正常返回这段的行数

return [self.dataArr[section]count];//返回行数,是大数组的每一个元素(小数组的长度)

}

else

{//当状态数组中该段为0时,这段行数就是0,不显示

return 0;

}

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *str =@"some";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:str];

if(cell == nil)

{

cell = [[[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str]
autorelease];

}

cell.textLabel.text =self.dataArr[indexPath.section][indexPath.row];

//从大数组中找到当前段indexPath.section中的当前行indexPath.row的内容

return cell;

}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

NSArray *headerArr =
@[@"好友",@"笔友",@"驴友",@"球友"];

UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

btn.frame = CGRectMake(0,0,
320, 40);

[btn setTitle:headerArr[section]forState:UIControlStateNormal];

[btn addTarget:selfaction:@selector(btnDown:)forControlEvents:UIControlEventTouchUpInside];

btn.backgroundColor = [UIColoryellowColor];

btn.tag = 1000+section;

return btn;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

NSLog(@"111");

return 40;

}

-(void)btnDown:(UIButton*)btn

{

//把statusArr数组对应下标的元素从1-0,从0-1

NSString *statusStr =
self.statusArr[btn.tag -1000];

[self.statusArrreplaceObjectAtIndex:btn.tag
-1000
withObject:[NSStringstringWithFormat:@"%d",![statusStrintValue]]];//把数组对应下标的元素转为整形,置反后转成字符串替换原来的

NSLog(@"%@",self.statusArr);

//table的刷新方法

UITableView *table = (UITableView*)[self.viewviewWithTag:6000];

[table reloadData];//刷新,每次调用这个方法,会整个重新执行一次这个table的所有代理方法

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: