UITableViewCell:自定義的單元格,能夠在xib中建立單元格,也能夠在storyBorad中建立單元格。有四種建立方式ide
1 #import <Foundation/Foundation.h> 2 3 @interface Contact : NSObject 4 @property (copy,nonatomic)NSString *name; 5 @property (copy,nonatomic)NSString *faceName; 6 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName; 7 @end
1 #import "Contact.h" 2 3 @implementation Contact 4 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName 5 { 6 self = [super init]; 7 if(self) 8 { 9 _name = [name copy]; 10 _faceName = [faceName copy]; 11 } 12 return self; 13 } 14 @end
在視圖控制器中完成代碼:(須要用tag獲取單元格的屬性控件)atom
1 #import "ViewController.h" 2 #import "Contact.h" 3 @interface ViewController ()<UITableViewDataSource> 4 @property (weak, nonatomic) IBOutlet UITableView *tableView; 5 @property (strong,nonatomic)NSMutableArray *contacts; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 //初始化數據 13 self.contacts = [NSMutableArray arrayWithCapacity:9]; 14 for(int i=0; i<9; i++) 15 { 16 Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+1] andFaceName:[NSString stringWithFormat:@"%d.png",i]]; 17 [self.contacts addObject:conatct]; 18 } 19 20 //設置tableView的數據源 21 self.tableView.dataSource = self; 22 } 23 24 #pragma mark -tableView的數據源方法 25 //每一組多少行 26 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 27 { 28 return self.contacts.count; 29 } 30 //設置每個單元格的內容 31 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 32 { 33 //1.根據reuseIdentifier,先到對象池中去找重用的單元格對象 34 static NSString *reuseIdentifier = @"myCell"; 35 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 36 //2.設置單元格對象的內容 37 Contact *contact = [self.contacts objectAtIndex:indexPath.row]; 38 UILabel *label = (UILabel*)[cell viewWithTag:1]; 39 label.text = contact.name; 40 UIImageView *imageView = (UIImageView*)[cell viewWithTag:2]; 41 [imageView setImage:[UIImage imageNamed:contact.faceName]]; 42 return cell; 43 } 44 45 @end
方法二:直接在storyBoard中建立單元格並關聯自定義的類並直接加載,自定義的單元格位置一個UITableView的上面spa
#import <Foundation/Foundation.h> @interface Contact : NSObject @property (copy,nonatomic)NSString *name; @property (copy,nonatomic)NSString *faceName; -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName; @end #import "Contact.h" @implementation Contact -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName { self = [super init]; if(self) { _name = [name copy]; _faceName = [faceName copy]; } return self; } @end
與單元格關聯的自定義的類,關聯單元格的屬性控件(不須要再用tag獲取了,直接用self.獲取)3d
仍是在視圖控制器中完成加載:代理
1 #import "ViewController.h" 2 #import "Contact.h" 3 #import "myTableViewCell.h" 4 @interface ViewController ()<UITableViewDataSource> 5 @property (weak, nonatomic) IBOutlet UITableView *tableView; 6 @property (strong,nonatomic)NSMutableArray *contacts; 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 //初始化數據 14 self.contacts = [NSMutableArray arrayWithCapacity:9]; 15 for(int i=0; i<9; i++) 16 { 17 Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+1] andFaceName:[NSString stringWithFormat:@"%d.png",i]]; 18 [self.contacts addObject:conatct]; 19 } 20 21 //設置tableView的數據源 22 self.tableView.dataSource = self; 23 } 24 25 #pragma mark -tableView的數據源方法 26 //每一組多少行 27 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 28 { 29 return self.contacts.count; 30 } 31 //設置每個單元格的內容 32 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 33 { 34 //1.根據reuseIdentifier,先到對象池中去找重用的單元格對象 35 static NSString *reuseIdentifier = @"myCell"; 36 myTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 37 //2.設置單元格對象的內容 38 Contact *contact = [self.contacts objectAtIndex:indexPath.row]; 39 cell.label.text = contact.name; 40 [cell.imgView setImage:[UIImage imageNamed:contact.faceName]]; 41 return cell; 42 } 43 44 @end
方法三:在xib文件中建立單元格,而後再視圖控制器中直接加載使用code
首先在storyBoard中添加一個UITableVieworm
而後在已經建立好的MyCell.xib中建立自定義的單元格爲:對象
設置該單元格的重用標識符identifier:blog
建立一個聯繫人初始化的類:繼承
1 #import <Foundation/Foundation.h> 2 3 @interface Contact : NSObject 4 @property (copy,nonatomic)NSString *name; 5 @property (copy,nonatomic)NSString *faceName; 6 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName; 7 @end 8 9 10 #import "Contact.h" 11 12 @implementation Contact 13 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName 14 { 15 self = [super init]; 16 if(self) 17 { 18 _name = [name copy]; 19 _faceName = [faceName copy]; 20 } 21 return self; 22 } 23 @end
仍是在視圖控制器中完成加載:
1 #import "ViewController.h" 2 #import "Contact.h" 3 #import "myTableViewCell.h" 4 @interface ViewController ()<UITableViewDataSource> 5 @property (weak, nonatomic) IBOutlet UITableView *tableView; 6 @property (strong,nonatomic)NSMutableArray *contacts; 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 //初始化數據 14 self.contacts = [NSMutableArray arrayWithCapacity:9]; 15 for(int i=0; i<9; i++) 16 { 17 Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+1] andFaceName:[NSString stringWithFormat:@"%d.png",i]]; 18 [self.contacts addObject:conatct]; 19 } 20 21 //設置tableView的數據源 22 self.tableView.dataSource = self; 23 } 24 25 #pragma mark -tableView的數據源方法 26 //每一組多少行 27 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 28 { 29 return self.contacts.count; 30 } 31 32 33 //直接從xib文件中加載 34 35 //設置每個單元格的內容 36 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 37 { 38 //1.根據reuseIdentifier,先到對象池中去找重用的單元格對象 39 static NSString *reuseIdentifier = @"myCell"; 40 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 41 //2.若是沒找到,就本身建立cell 42 if(!cell) 43 { 44 //從xib文件中加載視圖 45 NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"MyCell" owner:nil options:nil]; 46 cell = (UITableViewCell*)[views lastObject]; 47 } 48 //3.設置單元格對象的內容 49 Contact *contact = [self.contacts objectAtIndex:indexPath.row]; 50 UILabel *label = (UILabel*)[cell viewWithTag:1]; 51 label.text = contact.name; 52 UIImageView *imgView = (UIImageView*)[cell viewWithTag:2]; 53 [imgView setImage:[UIImage imageNamed:contact.faceName]]; 54 55 return cell; 56 }
方法四:在xib文件中建立單元格,並建立與之關聯的的類,而後將加載過程封裝到它的類中幫助初始化完成,同時該類提供類方法,最後再視圖控制器中經過這個類方法獲取單元格。
首先在storyBoard中添加一個UITableView
而後在已經建立好的MyCell.xib中建立自定義的單元格爲:
給單元格設置重用標識符identifier
將單元格與自定義的類關聯
建立一個聯繫人初始化的類:
1#import <Foundation/Foundation.h> 2 3 @interface Contact : NSObject 4 @property (copy,nonatomic)NSString *name; 5 @property (copy,nonatomic)NSString *faceName; 6 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName; 7 @end 8 9 10 #import "Contact.h" 11 12 @implementation Contact 13 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName 14 { 15 self = [super init]; 16 if(self) 17 { 18 _name = [name copy]; 19 _faceName = [faceName copy]; 20 } 21 return self; 22 } 23 @end
建立一個與單元格關聯的類:(將加載單元格的過程和屬性封裝起來)
在視圖控制器中經過上面的類方法獲取單元格
1 #import "ViewController.h" 2 #import "Contact.h" 3 #import "myTableViewCell.h" 4 @interface ViewController ()<UITableViewDataSource> 5 @property (weak, nonatomic) IBOutlet UITableView *tableView; 6 @property (strong,nonatomic)NSMutableArray *contacts; 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 //初始化數據 14 self.contacts = [NSMutableArray arrayWithCapacity:9]; 15 for(int i=0; i<9; i++) 16 { 17 Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+1] andFaceName:[NSString stringWithFormat:@"%d.png",i]]; 18 [self.contacts addObject:conatct]; 19 } 20 21 //設置tableView的數據源 22 self.tableView.dataSource = self; 23 } 24 25 #pragma mark -tableView的數據源方法 26 //每一組多少行 27 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 28 { 29 return self.contacts.count; 30 } 31 //在與xib關聯的類中加載xib文件(其實就是封裝了一下而已) 32 33 //設置每個單元格的內容 34 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 35 { 36 //1.根據reuseIdentifier,先到對象池中去找重用的單元格對象 37 static NSString *reuseIdentifier = @"myCell"; 38 myTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 39 //2.若是沒找到,就本身建立cell 40 if(!cell) 41 { 42 cell = [myTableViewCell cell];//調用類方法 43 } 44 //3.設置單元格對象的內容 45 Contact *contact = [self.contacts objectAtIndex:indexPath.row]; 46 [cell setContact:contact];//調用實例方法 47 48 return cell; 49 } 50 51 @end