UITableView使用及TableViewCell的复用机制



UI第十一天----UITableView

一、系统自带的UITable使用方法

          1.首先要在.h文件中包含(要显示需要将其设置为根视图来显示)UITableViewDataSource,UITableViewDelegate这两个协议。

//dataSource是控制显示内容的协议,里面的方法都为这个服务;delegate是控制交互的协议,里面的方法都为这个服务

//UISCrollView及其家族成员出现时,别忘了下面这句(除非你用了自动布局)

         2. 关闭自动调整高度方法

self.automaticallyAdjustsScrollViewInsets= NO;

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

   [self.view addSubview:table];

   [table release];

   table.dataSource = self;

   table.delegate = self;

   table.backgroundColor = [UIColor greenColor];

//设置table的行与行之间的分隔线。这里设置为没。

         3.将分隔线设置为无。

  table.separatorStyle = UITableViewCellSeparatorStyleNone;

     4. 设置这个table一共有多少段,如果不设置这个代理方法,默认就是1段。

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

//return多少就有几行。

   return  3;

}

     5. 告诉我们这个table的对应段一共有几行

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

{

   [super didReceiveMemoryWarning];

   //这里的参数section,表示这里的段。

   NSLog(@"%d",section);

   

   //表示我们的table 4行。

   //   return 3;

   //每一段的行数不一定非要一样,下面设置法可以控制每一段行数。默认为平均行数。

   if(section == 0)

   {

       return 0;

   }else if (section == 1)

   {

       return  1;

   }

   else

   {

       return 3;

   }

 

}

          6.下面方法中写每一行显示的内容,这种cell的用法不合适,只为举例:

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

{

   //tableView 这个参数表示正在被控制的table

   

 //这里的参数indexPath可以找到我们所在的段,也可以找到我们所在的行。

   

   int section = [indexPath section];//当前段

   int row = [indexPath row];//当前行

   

   //每一段里面的行数都是从0开始的,

   NSLog(@"这是第%d段的%d",section,row);

   

   //实例化了一个UITableViewCell的对象,这个对象,就是table的一行。他的功能就是作为这一行上显示的内容的父视图(在这个celladdSubView)。

   UITableViewCell *cell = [[UITableViewCell alloc] init];

   

   //cell自带文本属性

   //cell.textLabel.text = [NSStringstringWithFormat:@"%d--%d",[indexPath section],[indexPath row]];

   if([indexPath section]==0)

   {

   

     cell.textLabel.text = @"pp";

   }

   if([indexPath section]==1)

   {

   

    cell.textLabel.text = @"qq";

   }

   if([indexPath section]==2)

   {

       NSArray *tempArr = @[@"",@"",@""];

      cell.textLabel.text = tempArr[indexPath.row] ;

       //把所在行数与数组联系起来。

       //注意数组长度不能小于行数,不然会越界。

   

   }

   

   

   

   //cell自带的图片属性。

   cell.imageView.image=[UIImage imageNamed:@"0_s.png"];

   

   //设置cell的选中效果,这里设置为没效果。

   cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

}

 

7.两个必须实现的方法

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

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

二、真正的tableView,可以实现复用的tableView

1.同样先在.h文件中包含两个协议。并且声明一个全局的可变数组:

@property(nonatomic,retain)NSMutableArray *dataArr;

2.创建数据源,保存到可变数组。

Eg

//创建数据源

    for(int i = 0;i< 20;i++)//table同时加载一页的最多行数。

    {

        [self.dataArr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d本书",i+1],@"title",[NSString stringWithFormat:@"%d",i+10],@"price", nil]];

    //长度为20 的数组,每一个元素都是一个字典,每个字典都有相同的两个key,一个是title,一个是pricevalue都是字符串。

   

    }

3.创建视图

-(void)makeMainView

{

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

    table.dataSource = self;

    table.delegate = self;

    [self.view addSubview:table];

    [table release];

 

}

 

//返回行数

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

{

 

    return self.dataArr.count;

}

 

 

重点:下面模式为固定的。

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

{

   //UITableViewCell的复用

    static NSString *iden = @"iden";//字符串变量名和等号后面的常量都可随意,但要有意义。准备一个标识

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];//给这个tableview注册这个标识。,从此等号前的cell对象就用这个identableView里挂号了。

    if(cell== nil)//判空(就是当你想用cell的时候,发现cell不够用了,够用就不进ifif后面直接用)

    {

        //不够用,就创建新的。

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden]autorelease];//记得release

    

    }

    /*

      所谓复用

     1、我们只实例化屏幕显示所需的最大的个数

     2、当一个cell滚出屏幕时,这个cell会被扔进cell池里面

     3、当一个cell滚进屏幕时,回去cell池里面找是否有被扔进来的,如果找到了,就是用这个找到的cell(并把cell上之前的内容清空),如果没有找到,则实例化一个新的cell

     4、这种,使用之前创建好了cell的行为,就叫cell的复用。

     */

 

   

    for(id temp in cell.contentView.subviews)//找到cellcontentView上面原来所有子视图。

     {

        [temp removeFromSuperview];

    

     }

   

    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 34, 34)];

    image.backgroundColor = [UIColor redColor];

    //可以吧各种UIView添加到cell上面。

    ios6之前,可以直接添加到cell上,之后我们需要添加到cell.contentView

    //所以干脆就往cell.contentView上面添得了

    [cell.contentView addSubview:image];

    [image release];

   

   

//下面的视图控件为需要展示到每一行的东西。

    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 200, 25)];

    title.text = [NSString stringWithFormat:@"这是第%d",indexPath.row];

    [cell.contentView addSubview:title];

    [title release];

   

    UILabel *price = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 200, 19)];

    price.text = [NSString stringWithFormat:@"%d",6];

    [cell.contentView addSubview:price];

    [price release];

   

   

   

    return  cell;

}

 

//在此方法之下的诸多方法:

 

//设置每行多高的,瀑布流的时候每行高度应该是不一样的,随内容变化,一些固定布局,可以全一样高。默认44.

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

{

    return 80;

}

 

 

//当某一行被点击的时候触发

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

{

    NSLog(@"我将要去的页面的数据会从数组中的第%d的元素里取得",indexPath.row);

 

}

 

//设置每一段段头的文本内容,如果一个短只有一个文本内容的话,可以选择使用这个。

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

//{

//  return @"第一段";

//

//}

 

//设置每一段段头的视图,如果每一段的段头比较丰富的时候(有按钮、图片、label。。。),可以选择使用这个。

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

{

    UIView *view= [[UIView alloc]initWithFrame:CGRectMake(100, 0, 100, 20)];//不可控制frame

    view.backgroundColor = [UIColor brownColor];

   

   // return [view autorelease ];

   

   

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 60, 20)];

    label.text = @"";

    [view addSubview:label];//这样就可以控制frame

    return [view autorelease];

}

 

//设置段头高度。

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

{

    return 50;

 

}

 

//当一个已经被选中了的行,被取消选中时,触发

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

{

 

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

 

}

 

 

 

典例:如下图所示每一个tablecell里面有两个被点击的东西的实现:前期工作同上介绍。

QFDisk:Users:qianfeng:Desktop:屏幕快照 2014-08-18 19.01.58.png

.m文件中的主要方法:

- (void)viewDidLoad

{

    self.muArr = [NSMutableArray arrayWithCapacity:0];

    self.automaticallyAdjustsScrollViewInsets = NO;

    [super viewDidLoad];

    [self  loadData];

    [self makeMainView];

    //做一个有3段,3行的table。每一个上又两个BTN

          // Do any additional setup after loading theview, typically from a nib.

}

 

 

-(void)loadData

{

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

    {

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",arc4random()%10],@"btn",[NSString stringWithFormat:@"%d",i] ,@"label",nil];

        [self.muArr addObject:dic];

       

    }

   

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

}

 

 

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

{

   

    return  [self.muArr count];

}

 

 

-(void)makeMainView

{

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

    table.dataSource = self;

    table.delegate = self;

    [self.view addSubview:table];

    [table release];

   

   

   

}

 

//核心代码:

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

{

    static NSString *iden = @"iden";

   

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];

    if(cell== nil)

    {

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];

       

    }

   

    for(id temp in cell.contentView.subviews)

    {

       

        [temp removeFromSuperview];

       

    }

   

    //从数组中,根据当前行数对应下表吧数组里存的对应字典取出来。这个字典就是这一行要显示的内容。

   // NSDictionary *dicHere = self.muArr[indexPath.row];

   

   

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame = CGRectMake(10, 5, 34, 34);

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

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

    btn.tag = 1000+indexPath.row*2+100*indexPath.section;

//被标记为黑的tag设置方式出来每一段的tag值都会不一样。依次为100110021003 / 110111021103  / 120112021203

    NSLog(@"%d",btn.tag);

    [cell.contentView addSubview:btn];

   

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 25,60, 19)];

    label.text = @"" ;

    [cell.contentView addSubview:label];

    [label release];

   

   

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn1.frame = CGRectMake(200, 5, 60, 20);

    [btn1 setTitle:@"" forState:UIControlStateNormal];

    [btn1 addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:btn1];

    btn1.tag = 1000+indexPath.row*2+1+100*indexPath.section;

     NSLog(@"%d",btn1.tag);

    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200, 25, 60, 19)];

    label1.text = @"";

    [cell.contentView addSubview:label1];

    [label1 release];

 

   

    return  cell;

   

}

 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

 

    return 3;

}

 

-(void)btnDown:(UIButton *)btn

{

 

 NSLog(@"下面进入对应这个值的大数组里的第%d小数组的第%d个元素",(btn.tag-1000)/100,(btn.tag-1000)%100);

 

}



x源码下载:点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值