iphone 开发 自定义UITableViewCell的子类 ,轻松添加图片文本信息等

来源:互联网 发布:mvp 网络请求 编辑:程序博客网 时间:2024/05/17 02:34

转自http://blog.csdn.net/dongstone/article/details/7438254


有时候我们要使UITableView显示的数据更具可观性,更美化,就只能在视图控制器的.m文件中用代码一句一句地去写,这样就会需要繁杂大量的代码。不可否认,有时候人是很懒惰的,其中的最佳解决办法就是自定义一个UITableViewCell,控件可直接在上面拖拽,设置尺寸大小颜色等。

一:

1)首先创建一个空的.xib文件

2)创建UITableVeiwCell

3)修改继承类

4)创建输出口,并添加(拖拽)组件(图片,lable)等。可见其中组建可随意认定大小,位置尺寸,还有颜色。

5)设置lable的tag,方便以后的赋值。

哦,自定义UITableViewCell就算完成了,那么就让我们看看是怎么把他加到UITableView上的。

二:

1)其实这一步是创建项目时最先完成的。到这里才写,是为了讲解方便。 按正常创建一个表视图的步骤走,前面的文章已经提到过创表过程。看结果:


一个在正常不过的表了,不需要任何修改。

2)关键的部分是在控制器的.m文件。使刚才自定义的Cell呈现在UITableVeiw上。

   .h文件

[plain] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface MyUITableViewYourSelfCellControl : UIViewController <UITableViewDataSource,UITableViewDelegate> {  
  4.     UITableView *MyUITableView;  
  5.     UITableViewCell *MyTableViewCell;  
  6.     UITableViewCell *Mycell02;  
  7. }  
  8.   
  9. @property (nonatomic, strong) IBOutlet UITableView *MyUITableView;  
  10. @property (nonatomic, strong) IBOutlet UITableViewCell *MyTableViewCell;  
  11. @property(nonatomic,retain) NSArray *Mypeople;  
  12. @end  

  .m文件

[plain] view plaincopy
  1. #import "MyUITableViewYourSelfCellControl.h"  
  2.   
  3. @implementation MyUITableViewYourSelfCellControl  
  4. @synthesize MyUITableView;  
  5. @synthesize MyTableViewCell;  
  6. @synthesize Mypeople;  
  7. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  8. {  
  9.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  10.     if (self) {  
  11.         // Custom initialization  
  12.     }  
  13.     return self;  
  14. }  
  15.   
  16. - (void)didReceiveMemoryWarning  
  17. {  
  18.     // Releases the view if it doesn't have a superview.  
  19.     [super didReceiveMemoryWarning];  
  20.       
  21.     // Release any cached data, images, etc that aren't in use.  
  22. }  
  23.   
  24. #pragma mark - View lifecycle  
  25.   
  26. - (void)viewDidLoad  
  27. {  
  28.     //定义数据  
  29.       
  30.     NSDictionary *dic1=[[NSDictionary alloc] initWithObjectsAndKeys:@"dong",@"name",@"22",@"age", nil];    
  31.     NSDictionary *dic2=[[NSDictionary alloc] initWithObjectsAndKeys:@"wang",@"name",@"23",@"age", nil];    
  32.     NSDictionary *dic3=[[NSDictionary alloc] initWithObjectsAndKeys:@"zhang",@"name",@"24",@"age", nil];    
  33.     NSDictionary *dic4=[[NSDictionary alloc] initWithObjectsAndKeys:@"li",@"name",@"25",@"age", nil];    
  34.     NSDictionary *dic5=[[NSDictionary alloc] initWithObjectsAndKeys:@"sui",@"name",@"26",@"age", nil];   
  35.     NSArray *people=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil];  
  36.     self.Mypeople=people;  
  37.       
  38.     [super viewDidLoad];  
  39.     // Do any additional setup after loading the view from its nib.  
  40. }  
  41.   
  42. - (void)viewDidUnload  
  43. {  
  44.     [self setMyUITableView:nil];  
  45.     [self setMyTableViewCell:nil];  
  46.     [super viewDidUnload];  
  47.     // Release any retained subviews of the main view.  
  48.     // e.g. self.myOutlet = nil;  
  49. }  
  50.   
  51. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  52. {  
  53.     // Return YES for supported orientations  
  54.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  55. }  
  56. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  57.       
  58.     return [self.Mypeople count];  
  59. }  
  60.   
  61. -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  62.       
  63.     NSInteger row=[indexPath row];  
  64.      
  65.     static NSString *Tagtittle=@"dong";  
  66.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Tagtittle];  
  67.     if (cell==nil) {  
  68.         //最关键的就是这句。加载自己定义的nib文件  
  69.         NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil];  
  70.         //此时nib里含有的是组件个数  
  71.         if ([nib count]>0) {  
  72.             cell=self.MyTableViewCell;  
  73.         }  
  74.     }  
  75.       
  76.     NSDictionary *dic=[self.Mypeople objectAtIndex:row];  
  77.     NSString *name=[dic objectForKey:@"name"];  
  78.     NSString *age=[dic objectForKey:@"age"];  
  79.     //通过设定的tag值来找对应的lable,给其复制。  
  80.     UILabel *lablename=(UILabel*)[cell viewWithTag:1];  
  81.     lablename.text=name;  
  82.     UILabel *lableage=(UILabel*)[cell viewWithTag:2];  
  83.     lableage.text=age;  
  84.     return cell;  
  85. }  
  86. @end  

到这里我们需要了解一下bundle:是一个目录,包含了图像,声音,代码,nib文件,plist文件等应用程序的资源。

cocoa为我们提供了NSBundle类。其实我们的应用程序就是一个bundle。他也包含了如上面所述的资源。这里我们把它叫做mainbundle。

而此程序里这句 NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil];。加载nib文件。可以看做是先把Empty.xib加载到mainbundle里,等程序需要时再拿出来,赋给了这里的变量nib,然后就相当于把nib文件导进来一样,可以用其中的组件了。

运行效果:



原创粉丝点击