iOS:UITableViewCell自定義單元格

UITableViewCell:自定義的單元格,能夠在xib中建立單元格,也能夠在storyBorad中建立單元格。有四種建立方式ide

<1>在storyBorad中建立的單元格,它是靜態的單元格,單元格一開始就存在,能夠直接根據自定義的重用標識名加載使用;
<2>固然,storyBorad中單元格也能夠關聯一個自定義的類,這個類必須是繼承UITableViewCell,這種狀況下,直接根據自定義的重用標識名加載使用也是能夠的。
<3>在xib中建立的單元格,若是直接經過bundel的loadNibNme的方法加載,也能夠直接根據重用標識符加載使用;
<4>固然,xib文件中的單元格能夠關聯一個自定義的類,這個類必須是繼承UITableViewCell,這種狀況下,若是直接根據自定義的重用標識符加載使用是行不通的,由於此時代理的方法沒有對單元格對象進行初始化,此時,須要對建立單元格對象的過程封裝到本身關聯的類中進行,即一個建立的單元格的類方法用來加載xib文件,一個類對象的實例方法,用來設置單元格中屬性。
 
  
  這是一個相似於聯繫人表格的實例,有姓名和圖像,如下四種方式均可以實現:
 
  方法一:直接在storyBoard中建立單元格並直接加載,自定義的單元格位置一個UITableView的上面
  須要設置單元格的重用標識符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
複製代碼
複製代碼
 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

 
  須要設置單元格的重用標識符identifier
 
 
  將單元格與對應的自定義類關聯
 
  代碼以下:
爲初始化建立的一個類:
複製代碼
#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
相關文章
相關標籤/搜索