新手教程之使用Xib自定義UITableViewCell


新手教程之使用Xib自定義UITableViewCell

前言

首先:什么是UITableView?看圖

其次:什么是cell?

然后:為什么要自定cell,UITableView不是自帶的有cell么?

因為在日常開發中,系統自帶的cell滿足不了客戶和開發人員的需求(並且每個cell中的內容\大小\樣式相同),我們就需要自定義cell來實現更加優化的功能.比如下面這種

最后:怎么自定義cell?

1.創建一個新的項目,在storyboard中拖入兩個imageView,兩個label

 

 

2.在ViewController里面創建UITableView

 1 //
 2 //  ViewController.m
 3 //  Xib自定義UITableViewCell
 4 //
 5 //  Created by admin on 16/5/16.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
12 @property (nonatomic, strong) UITableView *tableView;
13 @end
14 
15 @implementation ViewController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     // Do any additional setup after loading the view, typically from a nib.
20     [self config];
21 }
22 
23 - (void)didReceiveMemoryWarning {
24     [super didReceiveMemoryWarning];
25     // Dispose of any resources that can be recreated.
26 }
27 
28 
29 -(void)config {
30     //初始化tableView,並給tableView設置frame以及樣式
31     self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
32     //遵守代理和數據源(因為要用到代理和數據源方法)
33     self.tableView.delegate = self;
34     self.tableView.dataSource = self;
35     //添加到ViewController的視圖中
36     [self.view addSubview:self.tableView];
37 }
38 
39 /**
40  *  返回多少個組(默認是1組,如果只有一組可以不實現這個方法)
41  *
42  *  @param tableView 當前tableView
43  *
44  *  @return 組的個數
45  */
46 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
47     return 1;
48 }
49 /**
50  *  每一組返回多少行
51  *
52  *  @param tableView 當前tableView
53  *  @param section   當前組
54  *
55  *  @return 行的個數
56  */
57 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
58     return 20;
59 }
60 
61 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
62     //指定cell的重用標識符
63     static NSString *reuseIdentifier = @"CELL";
64     //去緩存池找名叫reuseIdentifier的cell
65     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
66     //如果緩存池中沒有,那么創建一個新的cell
67     if (!cell) {
68         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
69     }
70     //返回當前cell
71     return cell;
72 }
系統自帶的UITableView

運行效果

4.Xib自定義cell

 

首先我們要創建一個xib文件,有兩種創建方式:

直接上圖

第一種:

第二種:

第二種在創建類的時候也同時創建了xib,比較方便,要是用第一種方式創建,還得關聯類

下面就要拖控件到Xib的cell中並給控件設置布局(約束)了

 

下面進入代碼階段

5.獲取Xib自定義的cell

代碼:(XibTableViewCell.h)

 1 //
 2 //  XibTableViewCell.h
 3 //  Xib自定義UITableViewCell
 4 //
 5 //  Created by admin on 16/5/16.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface XibTableViewCell : UITableViewCell
12 //加載xib的方法(自己寫的,不是系統自帶)
13 +(instancetype)xibTableViewCell;
14 
15 @end

(XibTableViewCell.m)

 1 //
 2 //  XibTableViewCell.m
 3 //  Xib自定義UITableViewCell
 4 //
 5 //  Created by admin on 16/5/16.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import "XibTableViewCell.h"
10 
11 @implementation XibTableViewCell
12 //實現類方法
13 +(instancetype)xibTableViewCell {
14     //在類方法中加載xib文件,注意:loadNibNamed:owner:options:這個方法返回的是NSArray,所以在后面加上firstObject或者lastObject或者[0]都可以;因為我們的Xib文件中,只有一個cell
15     return [[[NSBundle mainBundle] loadNibNamed:@"XibTableViewCell" owner:nil options:nil] lastObject];
16 }
17 
18 - (void)awakeFromNib {
19     // Initialization code
20 }
21 
22 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
23     [super setSelected:selected animated:animated];
24 
25     // Configure the view for the selected state
26 }
27 
28 @end

6.把xib文件加載到系統的UITableView中替換系統自帶的cell

這一步必須在

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中進行,或者是封裝在自己定義的類中,不過就算封裝了,也要在這個方法中調用.

6.1給控件拖線關聯到類中,方便調用控件

6.2代碼:

 1 //
 2 //  ViewController.m
 3 //  Xib自定義UITableViewCell
 4 //
 5 //  Created by admin on 16/5/16.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 //導入頭文件
11 #import "XibTableViewCell.h"
12 
13 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
14 @property (nonatomic, strong) UITableView *tableView;
15 @end
16 
17 @implementation ViewController
18 
19 - (void)viewDidLoad {
20     [super viewDidLoad];
21     // Do any additional setup after loading the view, typically from a nib.
22     [self config];
23 }
24 
25 - (void)didReceiveMemoryWarning {
26     [super didReceiveMemoryWarning];
27     // Dispose of any resources that can be recreated.
28 }
29 
30 
31 -(void)config {
32     //初始化tableView,並給tableView設置frame以及樣式
33     self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
34     //遵守代理和數據源(因為要用到代理和數據源方法)
35     self.tableView.delegate = self;
36     self.tableView.dataSource = self;
37     //添加到ViewController的視圖中
38     [self.view addSubview:self.tableView];
39 }
40 
41 /**
42  *  返回多少個組(默認是1組,如果只有一組可以不實現這個方法)
43  *
44  *  @param tableView 當前tableView
45  *
46  *  @return 組的個數
47  */
48 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
49     return 1;
50 }
51 /**
52  *  每一組返回多少行
53  *
54  *  @param tableView 當前tableView
55  *  @param section   當前組
56  *
57  *  @return 行的個數
58  */
59 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
60     return 20;
61 }
62 
63 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
64     //指定cell的重用標識符
65     static NSString *reuseIdentifier = @"CELL";
66     //去緩存池找名叫reuseIdentifier的cell
67     //這里換成自己定義的cell
68     XibTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
69     //如果緩存池中沒有,那么創建一個新的cell
70     if (!cell) {
71         //這里換成自己定義的cell,並調用類方法加載xib文件
72         cell = [XibTableViewCell xibTableViewCell];
73     }
74     //給cell賦值
75     cell.backView.image = [UIImage imageNamed:@"223733vuf3mhajhd04hdh5"];
76     cell.infoLabel.text = @"金三胖真帥";
77     cell.infoLabel.textColor = [UIColor redColor];
78     cell.zanView.image = [UIImage imageNamed:@"103112778vn00czp59p6w7"];
79     cell.zanLabel.text = @"100";
80     cell.zanLabel.textColor = [UIColor redColor];
81     //返回當前cell
82     return cell;
83 }
84 /**
85  *  返回cell的行高
86  *
87  *  @param tableView 當前tableView
88  *  @param indexPath
89  *
90  *  @return cell的行高
91  */
92 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
93     return 200;
94 }
95 
96 @end

 

運行效果

總結:到此Xib自定義UITableViewCell就告一段落,如有錯誤,煩請各位指正,希望能幫到大家,不是故意黑金三胖.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM