一、使用xib创建cell
Xib文件是一个轻量级的用来描述局部界面的文件,它与StoryBoard类似,都是使用Interface Bulider工具进行编辑。但是StoryBoard是重量级的,它是用来描述整个软件的多个界面,并且能够展示多个页面之间的跳转关系。在StoryBoard出现之前,一直是使用Xib文件表示界面。当然除了使用Xib文件,也可以直接使用代码来生成界面。
<一>、控制器自动添加xib
根据数据需求,简单拖动几个控件
对于什么是MVC,不再多说
Model.h
#import <Foundation/Foundation.h>
@interface MyModel : NSObject
@property (nonatomic, copy)NSString *price;
@property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *icon;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (id)foodWithDict:(NSDictionary *)dict;
@end
#import "MyModel.h"
@implementation MyModel
-(instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [self init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (id)foodWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
@end
FoodTableViewCell.h
#import <UIKit/UIKit.h>
#import "MyModel.h"
@interface FoodTableViewCell : UITableViewCell
@property (nonatomic, strong)MyModel *food;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@end
FoodTableViewCell.m
-(void)setFood:(MyModel *)food{
_food = food;
[self setData];
}
- (void)setData{
// self.iconView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_food.icon]]];
self.priceLabel.text = [NSString stringWithFormat:@"价格: %@",_food.price];
self.titleLabel.text = [NSString stringWithFormat:@"名字: %@",_food.name];
}
#import "ViewController.h"
#import "MyModel.h"
#import "FoodTableViewCell.h"
@interface ViewController ()<UITableViewDataSource>
{
UITableView *_tableView;
}
@property(nonatomic, strong)NSArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor orangeColor];
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// 调整边距,可以让表格视图让开状态栏
_tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
_tableView.rowHeight = 120;
}
- (NSArray *)dataArray{
if (_dataArray == nil) {
_dataArray = [[NSArray alloc]init];
NSString *path = [[NSBundle mainBundle]pathForResource:@"myPlist.plist" ofType:nil];
//获取全路径
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *mArray = [[NSMutableArray alloc]init];
for (NSDictionary *dict in array) {
MyModel *footModel = [MyModel foodWithDict:dict];
[mArray addObject:footModel];
}
_dataArray = mArray;
}
return _dataArray;
}
//实现数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"foodID";
FoodTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// cell = [[FoodTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
//xib 在资源文件中
cell = [[[NSBundle mainBundle]loadNibNamed:@"FoodTableViewCell" owner:nil options:nil]lastObject];
}
cell.food = self.dataArray[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
效果图简单如下: