自定义UITableViewCell

来源:互联网 发布:s8新赛季符文 知乎 编辑:程序博客网 时间:2024/05/19 13:22

1。在XCode中选择新建->User Interface->Empty XIB。(指定一个有意义的名字)

2。打开空的xib文件,将UITableViewCell拖到xib窗口中。

3。添加样式和其他控件到里面去,(UITextField & UITextView 不适用于表格视图单元)

4。打开属性检查器,设置重用标识符号Identifier,如:BaseTableCell

 自定义UITableViewCell - 木木 - 木木的博客
//选中Title lable,设置它的tag属性值为101,之后便可以利用自定义视图中Title lable的tag进行对cell的子视图的操作:

#define TEXTLABEL ((UILabel *)[cell viewWithTag:101])


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

//设置重用

      UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"BaseTableCell"];

      if(!cell)

{

cell = [[[NSBundle mainBundleloadNibNamed:@"BaseTableCell" owner:self options:nil]lastObject];

}

        //设置cell的背景

[cell setSelectedBackgroundView:[[UIImageView allocinitWithImage:[UIImageimageNamed:@"cell_bg.png"]]];

[TEXTLABEL setText:[[UIFont familyNamesobjectAtIndex:indexPath.row]];

return cell;

}


//可以在以上代理中设置单元格的高度 tableView.rowHeight = 100;

//也可以在专门设置高度的代理方法中设置表单元格高度

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

{

return 100;

}


//自定义单元格背景颜色

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

[cell setBackgroundColor:[UIColor redColor]];  //设置背景颜色

[cell setSelectedBackgroundView:[[UIImageView allocinitWithImage:[UIImageimageNamed:@"cell_bg.png"]]]; //设置选中后的背景,同前一个方法。

}


http://xiaohui3837843.blog.163.com/blog/static/54388740201151635857655/



----------------------------------------------------------------------------------


视图需要我们自己去定义样式时用到了TableViewCell组件,效果如下 
 
首先创建View Based App工程,在.xib文件中拖入一个Table View,前面我们说到了,这里就不再重复,注意连接协议和两个必须方法的实现。完成.h中代码 


  1. #import <UIKit/UIKit.h>  
  2. @interface TableViewCellViewController : UIViewController  
  3.             <UITableViewDelegate,UITableViewDataSource>{  
  4.                 IBOutlet UITableView *tView;  
  5. }  
  6. @property (nonatomic,retain)UITableView *tView;  
  7. @end  

完成.m中代码 


  1. #import "TableViewCellViewController.h"  
  2. #import "MyCell.h"  
  3. @implementation TableViewCellViewController  
  4. @synthesize tView;  
  5. - (void)viewDidLoad {  
  6.     [super viewDidLoad];  
  7. }  
  8. - (void)didReceiveMemoryWarning {  
  9.     [super didReceiveMemoryWarning];  
  10. }  
  11. - (void)dealloc {  
  12.     [tView release];  
  13.     [super dealloc];  
  14. }  
  15. -(NSInteger) tableView:(UITableView *)tableView  
  16.  numberOfRowsInSection:(NSInteger)section  
  17. {  
  18.     return 9;  
  19. }  
  20. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  21. {  
  22.     static NSString *CellIdentifier = @"CustomCellIdentifier";  
  23.     MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  24.     if (cell == nil) {  
  25.         NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil];  
  26.         cell = [array objectAtIndex:0];  
  27.         [cell setSelectionStyle:UITableViewCellSelectionStyleGray];  
  28.     }  
  29.     [[cell lable] setText:@"31"];  
  30.     [[cell lable1] setText:@"Raul"];  
  31.     [[cell myImage] setImage:[UIImage imageNamed:@"3316.jpg"]];  
  32.     return cell;  
  33. }  
  34. - (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath     
  35. {         
  36.     return 120;  
  37. }  
  38. @end  


下面我们新建一个类,注意subclass选择UITableViewCell,名称为MyCell,生成之后再创建相应的xib文件
 
 
双击MyCell.xib,将Table View Cell拖入主窗口中,并且删除原主窗口中的View图标 
 
在.h文件中完成代码 
C代码  收藏代码
  1. #import <UIKit/UIKit.h>  
  2. @interface MyCell : UITableViewCell {  
  3.     IBOutlet UILabel *lable;  
  4.     IBOutlet UILabel *lable1;  
  5.     IBOutlet UIImageView *myImage;  
  6. }  
  7. @property(nonatomic,retain) UILabel *lable;  
  8. @property(nonatomic,retain) UILabel *lable1;  
  9. @property (nonatomic,retain) UIImageView *myImage;  
  10. @end  

在.m中完成代码 
C代码  收藏代码
  1. #import "MyCell.h"  
  2. @implementation MyCell  
  3. @synthesize lable,lable1,myImage;  
  4. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {  
  5.     if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {  
  6.         // Initialization code  
  7.     }  
  8.     return self;  
  9. }  
  10. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {  
  11.   
  12.     [super setSelected:selected animated:animated];  
  13.   
  14.     // Configure the view for the selected state  
  15. }  
  16. - (void)dealloc {  
  17.     [myImage release];  
  18.     [lable release];  
  19.     [super dealloc];  
  20. }  
  21. @end  

最后我们看一下MyCell.xib中的连接,按住Ctrl拖入将要显示区域的文字和图片找到相应的接口即可。(注意:是myCell和文字和图片连接,不是file’s owner和文字和图片连接,我又犯这个错误了) 
 





原创粉丝点击