定製單元格-cell

在咱們接觸到的app中,大部分的都是有tableView來顯示信息的,如此,就須要有TableViewCell進行數據的顯示。下面介紹3種定製的方法。數組

首先建立一個BaseTableViewController繼承於UITableViewController,並顯示數據,點擊cell跳轉到對應的controller,顯示數據。app

其中Model數據代碼以下:ide

@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *time;
@property(nonatomic,copy)NSString *commentCount;

 

加載主界面

接下來建立3個類 "FirsetTableViewController.h"、 "SecondTableViewController.h"、 "ThirdTableViewController.h" and "Model.h",並導入頭文件。佈局

代碼以下:atom

//數據存儲
@property(nonatomic,strong)NSMutableArray *modelDataList;

 

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3 //  添加數據
 4     [self loadData];
 5 }
 6 
 7 - (void)loadData{
 8     //獲取路徑文件
 9     NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];
10     NSMutableArray *dataArr = [NSMutableArray arrayWithContentsOfFile:path];
11     //首先建立空間
12     _modelDataList = [[NSMutableArray alloc] init];
13     //將讀出的數據轉化爲對象
14     for (NSDictionary *dataDic in dataArr) {
15         NewModel *model = [[NewModel alloc] init];
16         model.title = [dataDic objectForKey:@"title"];
17         model.commentCount = [dataDic objectForKey:@"commentCount"];
18         model.time = [dataDic objectForKey:@"time"];
19         //將建立的數據加入到數組中
20         [_modelDataList addObject:model];
21     }
22     
23 }
24 
25 #pragma mark - Table view data source
26 //默認爲1
27 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
28 
29     return 1;
30 }
31 //返回的行數
32 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
33 
34     return 3;
35 }
36 
37 //cell的重用
38 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
39     //惟一識別標誌 identifier
40     static NSString *identifier = @"baseCell";
41     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
42     if (cell == nil) {
43         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
44     }
45     //顯示內容
46     cell.textLabel.text = [NSString stringWithFormat:@"第%ld種單元格定製方法",indexPath.row + 1];
47     return cell;
48 }
49 //選中調用的方法
50 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
51     if (indexPath.row == 0) {
52         FirsetTableViewController *firstVC = [[FirsetTableViewController alloc] init];
53         //-----數據傳送----
54         firstVC.modelArray = self.modelDataList;
55         [self.navigationController pushViewController:firstVC animated:YES];
56     }
57     if (indexPath.row == 1) {
58         //有ib的寫法
59         SecondTableViewController *secVC = [[SecondTableViewController alloc] init];
60         secVC.modelArray = self.modelDataList;
61         [self.navigationController pushViewController:secVC animated:YES];
62     }
63     if (indexPath.row == 2) {
64         ThirdTableViewController *thirdVC = [[ThirdTableViewController alloc] init];
65         thirdVC.modelArray = self.modelDataList;
66         [self.navigationController pushViewController:thirdVC animated:YES];
67     }
68 }

 

第一種定製方法

  1. 設置一個數據z存放可變數組

    @property(nonatomic,strong)NSMutableArray *modelArray;spa

  2. 導入Model.h。在主界面加載後就進行了數據解析。3d

  3. 根據plist文件數據進行代碼顯示。以下:
     1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     2     return 1;
     3 }
     4 //返回的行數,數組中元素的個數
     5 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     6     return _modelArray.count;
     7 }
     8 
     9 //cell重用
    10 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    11     static NSString *identifier = @"cell";
    12     UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    13     if (cell == nil) {
    14         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    15         //建立標題視圖
    16         UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
    17         titleLabel.tag = 101;
    18         //添加到單元格
    19         [cell.contentView addSubview:titleLabel];
    20         //建立評論數標題
    21         UILabel *commentCount =[[UILabel alloc] initWithFrame:CGRectMake(10, 50, 100, 30)];
    22         commentCount.tag =102;
    23         [cell.contentView addSubview:commentCount];
    24         
    25         //建立時間標題
    26         UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 50, 160, 30)];
    27         timeLabel.tag = 103;
    28         [cell.contentView addSubview:timeLabel];
    29         
    30     }
    31     //獲取當前row下的model,根據tag值來添加相應的數據
    32     NewModel *model = self.modelArray[indexPath.row];
    33     UILabel *titleLabel = (UILabel*)[cell viewWithTag:101];
    34     titleLabel.text = model.title;    
    35     UILabel *commentLabel = (UILabel*)[cell viewWithTag:102];
    36     commentLabel.text = [NSString stringWithFormat:@"評論數:%@",model.commentCount];
    37     UILabel *timeLabel = (UILabel*)[cell viewWithTag:103];
    38     timeLabel.text = [NSString stringWithFormat:@"在%@小時前發佈消息",model.time];
    39     return cell;
    40 }
    41 //行高,不肯定的高度,可本身定義
    42 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    43     
    44     return 100;
    45 }

第二種定製方法

這種方法是使用的.xib。code

代碼以下:(重複代碼再也不書寫:行數、行高同方法一)須要注意的就是xib的使用。orm

 1 //設置cell
 2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 3     static NSString *identifier = @"myCell";
 4     UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
 5     if (cell == nil) {
 6     // 重用cell添加
 7         cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil] lastObject];
 8     }
 9     //獲取對應的model
10     NewModel *model = [_modelArray objectAtIndex:indexPath.row];
11     //設置屬性
12     UILabel *titleLa = (UILabel*)[cell viewWithTag:103];
13     titleLa.text = [NSString stringWithFormat:@"標題:%@",model.title];
14     UILabel *timeLa = (UILabel*)[cell viewWithTag:102];
15     timeLa.text = [NSString stringWithFormat:@"在%@小時前發佈消息",model.time];
16     UILabel *commLa = (UILabel*)[cell viewWithTag:101];
17     commLa.text = [NSString stringWithFormat:@"評論數:%@",model.commentCount];
18     return cell;
19 }

第三種定製方法(MVC)

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 2     static NSString *idetifier = @"myCell";
 3     MyTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:idetifier];
 4     if (cell == nil) {
 5         cell = [[[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil] lastObject];
 6     }
 7     NewModel *model = self.modelArray[indexPath.row];
 8     
 9     /*
10     //在外部設置
11     cell.timeLa.text = model.time;
12     cell.titleLa.text = model.title;
13     cell.commentCountLa.text = model.commentCount;
14      */
15     
16     //內部設置
17     cell.model = model;
18     return cell;
19 }

其中在設置xib的時候,能夠使用autolayout佈局。對象

而在cell中的數據傳值代碼則簡單的很。不用多解釋 IBOutlet 是什麼意思了。如下是.h中

1 @property (weak, nonatomic) IBOutlet UILabel *commentCountLa;
2 @property (weak, nonatomic) IBOutlet UILabel *timeLa;
3 @property (weak, nonatomic) IBOutlet UILabel *titleLa;
4 @property(nonatomic,strong)NewModel *model;

在.m中代碼,重寫了 layoutSubviews 方法。

 1 - (void)awakeFromNib {
 2     //對於xib的設置,就是有awaekeFromNib 獲得
 3 }
 4 
 5 - (void)layoutSubviews {
6 7 //內部設置 對應 cell.model = model; 8 [super layoutSubviews]; 9 self.titleLa.text = self.model.title; 10 self.timeLa.text = self.model.time; 11 self.commentCountLa.text = self.model.commentCount; 12 13 }

效果圖:

以上只是對不一樣cell的定製,簡單的方法。大部分使用仍是在tableView中的。如若想了解tableView,點擊

對於不一樣的值傳遞,利用MVC時最好的模式。在cell中只要重寫set方法就能夠了。

 

 

歡迎各位讀者閱讀,錯誤請指正。如若轉載,請標明出處。

相關文章
相關標籤/搜索