iOS开发--自定义cell基本实现

一、使用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;
}

效果图简单如下:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值