### 有時候我們要使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
for (UIView *v in nib) {
[v isKindOfClass:[UITableViewCell class]]?(cell=(UITableViewCell *)v):nil;
}
}
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文件導進來一樣,可以用其中的組件了。
運行效果:

- 前言
- (1) iphone開發,自定義Window-based Application 模板及委托運行機制
- (2) iphone 開發 表視圖UITableView結構深層剖析
- (3) iphone 開發 從應用程序看UITableView的:分組,分區,索引,工作原理及其變換法則,plist文件數據定義規則
- (4) iphone 開發 自定義UITableViewCell的子類 ,輕松添加圖片文本信息等
- (5) iphone 開發 在表視圖(UITableView) 中利用UISearchBar實現數據的搜索,視圖的多功能化
- (6) iphone 開發 真正理解委托(delegate)與數據源(data source)
- (7)---01 iphone 開發 數據傳遞 NSNotification 通知機制演示
- (7)---02 iphone 開發 數據傳遞 : 頁面切換與數據的反向傳遞以及協議(protocol)作用的體現
- (8)---01 iphone 開發 大話分析導航欄NavigationController
- (9) iphone 開發 AppSettings , 系統setting與應用程序setting間的數據控制
- (10) iphone 開發 用戶點擊,觸摸和手勢識別 解析