年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2685|回复: 0

用xib创建TableViewCell,熟悉MVC模型

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-4-9 21:05:52 | 显示全部楼层 |阅读模式
    1、效果展示

    iOS 模拟器屏幕快照“2014年4月9日 下午7.30.26”.png

    2、设计说明

    2.0 准备相关展示数据,编写成plist文件
    2.1 删除之前的控制器,新建一个类SATableViewController,继承UITableViewController;(MVC中的Contrller)
    2.2 创建一个xib,设计一个UITableViewCell的基本样式;
    QQ20140409-1@2x.png
    2.3 创建一个类SACarCell,该类用来加载xib,并给xib中各控件赋值;(MVC中的View)
    2.4 创建一个类SACar,该类用来创建数据模型;(MVC中的Model)
    2.5 控制器操作:2.5.1 通过数组加载plist文件中的数据,再用模型将数据格式化;
    2.5.2 实现DataSource协议的两个方法:表格行数,表格Cell内容
    2.6 模型操作:
    2.6.1 创建数据的各个属性;
    2.6.2 实现一个类方法,将plist文件中的数据转换成模型数据;
    2.7 视图操作:
    2.7.1 类方法加载xib文件中的基本视图
    2.7.2 xib中的各个view抽象成属性
    2.7.3 将模型抽象成视图的一条属性
    2.7.4 重写模型属性的set方法,让视图结合模型将数据自动展示;

    3、关键代码
    3.1 SACar.h
    1. //
    2. //  SACar.h
    3. //  XibCell
    4. //
    5. //  Created by yusian on 14-4-9.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface SACar : NSObject
    10. @property (nonatomic, copy) NSString *icon;
    11. @property (nonatomic, copy) NSString *title;
    12. @property (nonatomic, copy) NSString *source;
    13. @property (nonatomic, copy) NSString *price;
    14. - (id)initWithDict:(NSDictionary *)dict;
    15. + (id)carWithDict:(NSDictionary *)dict;
    16. @end
    复制代码
    3.2 SACar.m
    1. //
    2. //  SACar.m
    3. //  XibCell
    4. //
    5. //  Created by yusian on 14-4-9.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SACar.h"
    9. @implementation SACar
    10. - (id)initWithDict:(NSDictionary *)dict
    11. {
    12.     if (self = [super init]) {
    13.         self.icon = dict[@"Icon"];
    14.         self.title = dict[@"Title"];
    15.         self.source = dict[@"Source"];
    16.         self.price = dict[@"Price"];
    17.     }
    18.     return self;
    19. }
    20. + (id)carWithDict:(NSDictionary *)dict
    21. {
    22.     return [[self alloc] initWithDict:dict];
    23. }
    24. @end
    复制代码
    3.3 SACarCell.h
    1. //
    2. //  SACarCell.h
    3. //  XibCell
    4. //
    5. //  Created by yusian on 14-4-9.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <UIKit/UIKit.h>
    9. @class SACar;
    10. @interface SACarCell : UITableViewCell
    11. @property (nonatomic, weak) IBOutlet UILabel *titleLable;
    12. @property (nonatomic, weak) IBOutlet UILabel *priceLable;
    13. @property (nonatomic, weak) IBOutlet UILabel *sourceLable;
    14. @property (nonatomic, weak) IBOutlet UIImageView *iconView;
    15. @property (nonatomic, strong) SACar *car;
    16. + (id)carCell;
    17. + (NSString *)ID;
    18. @end
    复制代码
    3.4 SACarCell.m
    1. //
    2. //  SACarCell.m
    3. //  XibCell
    4. //
    5. //  Created by yusian on 14-4-9.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SACarCell.h"
    9. #import "SACAr.h"
    10. @implementation SACarCell
    11. + (id)carCell
    12. {
    13.     return [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:nil options:nil][0];
    14. //    UINib *nib = [UINib nibWithNibName:@"Cell" bundle:nil];
    15. //    return [nib instantiateWithOwner:nil options:nil][0];
    16. }
    17. - (void)setCar:(SACar *)car
    18. {
    19.     _car = car;
    20.     _iconView.image = [UIImage imageNamed:car.icon];
    21.     _priceLable.text = car.price;
    22.     _sourceLable.text = [NSString stringWithFormat:@"来源:%@", car.source];
    23.     _titleLable.text = car.title;
    24. }
    25. + (NSString *)ID;
    26. {   
    27.     return @"ID";
    28. }
    29. @end
    复制代码
    3.5 SATableViewController.h
    1. //
    2. //  SATableViewController.h
    3. //  XibCell
    4. //
    5. //  Created by yusian on 14-4-4.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <UIKit/UIKit.h>
    9. #import "SACar.h"
    10. #import "SACarCell.h"
    11. @interface SATableViewController : UITableViewController
    12. @end
    复制代码
    3.6 SATableViewController.m
    1. //
    2. //  SATableViewController.m
    3. //  XibCell
    4. //
    5. //  Created by yusian on 14-4-4.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SATableViewController.h"
    9. @interface SATableViewController ()
    10. {
    11.     NSMutableArray *_cars;
    12. }
    13. @end
    14. @implementation SATableViewController
    15. #pragma mark - View加载后调用
    16. - (void)viewDidLoad
    17. {
    18.     [super viewDidLoad];
    19.    
    20.     self.tableView.rowHeight = 80;  // 如果每行固定高度可用该属性设置cell高度
    21.    
    22.     NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data.plist" ofType:nil]];    // 将plist文件数据加载到数组
    23.    
    24.     _cars = [NSMutableArray array]; // 初始化用来存储模型数据的数组
    25.    
    26.     for (NSDictionary *dict in array) {
    27.         [_cars addObject:[SACar carWithDict:dict]];
    28.     }   // 将模型数据加载到数组当中
    29.    
    30. }
    31. #pragma mark - Table view data source
    32. // 返回表格行数
    33. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    34. {
    35.     return [_cars count];   // 数组长度即为表格行数
    36. }
    37. // 返回表格各行中内容
    38. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    39. {
    40.     // 回收标记的cell,遵循封装的原则,将ID写进视图的类方法来实现
    41.     SACarCell *cell = [tableView dequeueReusableCellWithIdentifier:[SACarCell ID]];
    42.    
    43.     if (nil == cell) {
    44.         cell = [SACarCell carCell];     // 调用视图的类方法创建一个cell
    45.     }
    46.    
    47.     cell.car = _cars[indexPath.row];    // 将cell对应的模型取出展示到视图中
    48.    
    49.     return cell;
    50. }
    51. @end
    复制代码
    4、源代码下载:
    游客,本帖隐藏的内容需要积分高于 1 才可浏览,您当前积分为 0




    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-3 20:29 , Processed in 0.053252 second(s), 26 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表