分享

UITableView的基本使用三(自定义Cell)

 quasiceo 2014-04-16

   

   

       
        分类:
            IOS UI基础
       

    2014-02-16 16:34
    222人阅读
    评论(0)
    收藏
    举报
   



   


在项目中,我们经常会使用UITableView,但是tableView中的cell格式又不一定每次都是一样的,所以我们需要自己实现我们自定义的cell,而自定义cell的方式也由很多,我们先采取用Xib的方式。



1.通过xib创建一个cell,并且通过tag来获取cell上的其他子元素,这种方式会产生很多的tag,让tag都暴漏在控制器中,不方便管理,而且viewWithTag性能低下,每次使用的时候,会遍历所有tag。


部分代码:



  1. // tag比较多不容易管理    
  2. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  3.   
  4.    static NSString * ID=@"CELL";  
  5.      
  6.    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];  
  7.    if (cell==nil) {  
  8.       // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:ID];  
  9.        //通过xib文件加载cell  
  10.       cell= [[[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:nil options:nil] lastObject];  
  11.         
  12.        //绑定监听器  
  13.        UIButton *cool=(UIButton *)[cell viewWithTag:3];  
  14.        [cool addTarget:self action:@selector(coolectBook:event:) forControlEvents:UIControlEventTouchUpInside];  
  15.    }  
  16.      
  17.    //覆盖shuj  
  18.    Book *book=self.books[indexPath.row];  
  19.      
  20.    //拿出第一个cell  
  21.    UILabel *nameLable=(UILabel *)[cell viewWithTag:1];  
  22.    nameLable.text=book.name;  
  23.      
  24.    UILabel *priceLable=(UILabel *)[cell viewWithTag:2];  
  25.      
  26.    priceLable.text=[NSString stringWithFormat:@"¥%.1f",book.price];  
  27.    NSLog(@"%p",cell);  
  28.      
  29.    return cell;  


通过触控点的位置获取所在行



  1. //根据位置获取所在的行  
  2. -(void)coolectBook:(UIButton *)btn event:(UIEvent *)event //包含所有的触摸点  
  3. {  
  4.     UITableView *tableView=(UITableView *)self.view;  
  5.     NSSet *touches = [event allTouches]; //包含所有的uitouch 单点触控就只有一个uitouch  
  6.       
  7.     UITouch *touch= [touches anyObject];  
  8.     //获取触摸点在uitableVIEW上的位置  
  9.     CGPoint p=[touch locationInView:tableView];  
  10.     NSIndexPath *path= [tableView indexPathForRowAtPoint:p];  
  11.     Book *book=self.books[path.row];  
  12.     NSLog(@"row--%@",book.name);  
  13.   
  14. }  





2.利用loadNibNamed加载xib文件的时候把控制器传入owner,让xib的file's owner的custom class为控制器,也就是控制器充当监听器,这样控制器就可以监听到自定义cell的方法,这样做,耦合度比较高,不利于维护和扩展,自定义的cell只能被一个控制器使用。























部分代码:



  1. NSArray *objs= [[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil] ;  


3.我们自己以面向对象的方式,把关于自定义cell的所有的元素都封装在一个类里面,写一个子类继承自UITableViewCell,并把xib创建的cell的custom class改为子类的名称,这样我们加载xib生成的cell对象就是我们的子类对象。






封装的子类cell



  1. #import <UIKit/UIKit.h>  
  2.   
  3. /* 
  4.   
  5.  把一个cell上的元素封装在一个整体 
  6.  只提供get方法 
  7.  */  
  8.    
  9. @interface BookCell : UITableViewCell  
  10. @property (weak, nonatomic,readonly) IBOutlet UILabel *nameLable;  
  11. @property (weak, nonatomic,readonly) IBOutlet UILabel *priceLable;  
  12. @property (weak, nonatomic,readonly) IBOutlet UIButton *BuyBtn;  
  13.   
  14. @end  


注意:这里我们要使用readonly让外界只能访问我们内部的组件。



  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.       
  3.     static NSString * ID=@"CELL";  
  4.       
  5.     BookCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];  
  6.     if (cell==nil) {  
  7.         // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:ID];  
  8.         //通过xib文件加载cell  
  9.         NSArray *objs= [[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil] ;  
  10.           
  11.         NSLog(@"%@",[objs lastObject]);  
  12.           
  13.         cell=[objs lastObject];  
  14.         //绑定监听器  
  15.         
  16.           
  17.         //为子类的btn对象绑定事件  
  18.         [cell.BuyBtn addTarget:self action:@selector(buyBook) forControlEvents:UIControlEventTouchUpInside];  
  19.     }  
  20.       
  21.     //覆盖数据  
  22.     Book *book=self.books[indexPath.row];  
  23.       
  24.     cell.nameLable.text=book.name;  
  25.     cell.priceLable.text=[NSString stringWithFormat:@"%.1f",book.price];  
  26.       
  27.     return cell;  
  28. }  
通过这种方式,我们自定义的cell很灵活,就像一个插件一样,如果其他地方要使用,只用导入我们的.h文件就可以了。


4.纯代码添加自定义的cell,子类会重写父类的initWithStyle方法用来初始化cell的样式


.h文件


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface MsgCell : UITableViewCell  
  4. @property(nonatomic,weak,readonlyUIImageView * icon;  
  5. @property(nonatomic,weak,readonlyUIButton * messageBtn;  
  6. @end  



  1. #import "MsgCell.h"  
  2.   
  3. @implementation MsgCell  
  4.   
  5. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  6. {  
  7.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  8.     if (self) {  
  9.         UIImageView *icon=[[UIImageView alloc]init];  
  10.         icon.frame=CGRectMake(10105050);  
  11.         [self.contentView addSubview:icon];  
  12.         //[self addSubview:icon];  
  13.         _icon=icon;  
  14.           
  15.         UIButton *btn=[[UIButton alloc]init];  
  16.         btn.frame=CGRectMake(801020060);  
  17.         [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_focused.png"] forState:UIControlStateHighlighted];  
  18.         [btn setBackgroundImage:[UIImage imageNamed:@"chatfrom_bg_normal.png"] forState:UIControlStateNormal];  
  19.         [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  20.         [self.contentView addSubview:btn];  
  21.         _messageBtn=btn;  
  22.     }  
  23.     return self;  
  24. }  
  25. @end  
  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  2.      
  3.     static NSString *ID=@"cell";  
  4.     MsgCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];  
  5.     if (cell==nil) {  
  6.         cell=[[MsgCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];  
  7.     }  
  8.       
  9.     cell.icon.image=[UIImage imageNamed:@"1.jpg"];  
  10.     [cell.messageBtn setTitle:@"dfgdfgdfg" forState:UIControlStateNormal];  
  11.      
  12.     return cell;  
  13. }  




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多