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文件
- #import <UIKit/UIKit.h>
-
- @interface MyUITableViewYourSelfCellControl : UIViewController <UITableViewDataSource,UITableViewDelegate> {
- UITableView *MyUITableView;
- UITableViewCell *MyTableViewCell;
- UITableViewCell *Mycell02;
- }
-
- @property (nonatomic, strong) IBOutlet UITableView *MyUITableView;
- @property (nonatomic, strong) IBOutlet UITableViewCell *MyTableViewCell;
- @property(nonatomic,retain) NSArray *Mypeople;
- @end
.m文件
- #import "MyUITableViewYourSelfCellControl.h"
-
- @implementation MyUITableViewYourSelfCellControl
- @synthesize MyUITableView;
- @synthesize MyTableViewCell;
- @synthesize Mypeople;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
-
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
-
- // Release any cached data, images, etc that aren't in use.
- }
-
- #pragma mark - View lifecycle
-
- - (void)viewDidLoad
- {
- //定义数据
-
- NSDictionary *dic1=[[NSDictionary alloc] initWithObjectsAndKeys:@"dong",@"name",@"22",@"age", nil];
- NSDictionary *dic2=[[NSDictionary alloc] initWithObjectsAndKeys:@"wang",@"name",@"23",@"age", nil];
- NSDictionary *dic3=[[NSDictionary alloc] initWithObjectsAndKeys:@"zhang",@"name",@"24",@"age", nil];
- NSDictionary *dic4=[[NSDictionary alloc] initWithObjectsAndKeys:@"li",@"name",@"25",@"age", nil];
- NSDictionary *dic5=[[NSDictionary alloc] initWithObjectsAndKeys:@"sui",@"name",@"26",@"age", nil];
- NSArray *people=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil];
- self.Mypeople=people;
-
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
-
- - (void)viewDidUnload
- {
- [self setMyUITableView:nil];
- [self setMyTableViewCell:nil];
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- return [self.Mypeople count];
- }
-
- -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- NSInteger row=[indexPath row];
-
- static NSString *Tagtittle=@"dong";
- UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Tagtittle];
- if (cell==nil) {
- //最关键的就是这句。加载自己定义的nib文件
- NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil];
- //此时nib里含有的是组件个数
- if ([nib count]>0) {
- cell=self.MyTableViewCell;
- }
- }
-
- NSDictionary *dic=[self.Mypeople objectAtIndex:row];
- NSString *name=[dic objectForKey:@"name"];
- NSString *age=[dic objectForKey:@"age"];
- //通过设定的tag值来找对应的lable,给其复制。
- UILabel *lablename=(UILabel*)[cell viewWithTag:1];
- lablename.text=name;
- UILabel *lableage=(UILabel*)[cell viewWithTag:2];
- lableage.text=age;
- return cell;
- }
- @end
到这里我们需要了解一下bundle:是一个目录,包含了图像,声音,代码,nib文件,plist文件等应用程序的资源。
cocoa为我们提供了NSBundle类。其实我们的应用程序就是一个bundle。他也包含了如上面所述的资源。这里我们把它叫做mainbundle。
而此程序里这句 NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil];。加载nib文件。可以看做是先把Empty.xib加载到mainbundle里,等程序需要时再拿出来,赋给了这里的变量nib,然后就相当于把nib文件导进来一样,可以用其中的组件了。
运行效果: