iOS技术点一群程序猿的秘密基地mas

屏幕适配+懒加载+MVC+KVC+多选删除

2016-06-12  本文已影响934人  i赵磊
《[一个程序猿的秘密基地](http://www.jianshu.com/collection/7b76c71b2d73?utm_campaign=maleskine&utm_content=collection&utm_medium=reader_share&utm_source=weibo)》专题

【本文章只为给初学者阅读使用,大牛绕行】(@^_^@)

ViewController.h
//此两宏是Masonry的辅助宏
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS

#import "ViewController.h"
#import <Masonry.h>
#import "ZLShopModel.h"
#import "ZLTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(weak,nonatomic)UITableView *tableView;
/** plist的model数据可变数组*/
@property(strong,nonatomic)NSMutableArray *modelArray_M;
@end

@implementation ViewController

/** 懒加载获取plist文件的数据,并把数据转换成模型存入数组*/ //TODO:modelArray的懒加载
-(NSMutableArray *)modelArray_M{
    if (!_modelArray_M) {
        NSArray *array=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs" ofType:@"plist"]];
        NSMutableArray *temporaryArray=[[NSMutableArray alloc]init];
        for (int a=0; a<array.count; a++) {
            ZLShopModel *model=[ZLShopModel shopModelWithDictionary:array[a]];
            [temporaryArray addObject:model];
        }
        _modelArray_M=temporaryArray;
    }
    return _modelArray_M;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTableView];
    [self addButtonWithTitle:@"进入编辑状态" LeftOrRightConstraint:@"left" Tag:1];
    [self addButtonWithTitle:@"删除" LeftOrRightConstraint:@"right" Tag:2];
}

/** 添加TableView,并对它进行约束*/
-(void)addTableView{
    UITableView *tableView=[[UITableView alloc]init];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    tableView.rowHeight=100.f;
    tableView.dataSource=self;
    tableView.delegate=self;
    __weak typeof(self)weakSelf=self;
    [tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(50);
        make.left.equalTo(weakSelf.view);
        make.width.equalTo(weakSelf.view);
        make.height.equalTo(weakSelf.view).offset(-50);
    }];
}

#pragma mark- UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray_M.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZLTableViewCell *cell=[ZLTableViewCell cellForTableView:tableView];
    cell.model=self.modelArray_M[indexPath.row];
    return cell;
}

/** 此方法用来创建顶部的按钮-->
 *title:按钮的文字
 *LeftOrRightConstraint:选择left或者是right(字符串类型)
 */
-(UIButton *)addButtonWithTitle:(NSString*)title LeftOrRightConstraint:(NSString *)constraint Tag:(NSInteger)tag{
    UIButton *button=[[UIButton alloc]init];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor=[constraint isEqualToString:@"left"]?[UIColor blueColor]:[UIColor redColor];
    [self.view addSubview:button];
    button.tag=tag;
    __weak typeof(self)weakSelf=self;
    [button makeConstraints:^(MASConstraintMaker *make) {
        [constraint isEqualToString:@"left"]?make.left.equalTo(weakSelf.view):make.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view);
        make.width.equalTo(150);
        make.height.equalTo(50);
    }];
    return button;
}

/** 顶部按钮的点击事件*/ //TODO:顶部按钮的点击事件
-(void)buttonClick:(UIButton *)sender{
    if (sender.tag==1) {
        //允许在编辑模式进行勾选操作
        self.tableView.allowsMultipleSelectionDuringEditing=YES;
        self.tableView.editing=!self.tableView.editing;
        return;
    }
    NSArray *array=[self.tableView indexPathsForSelectedRows];
    NSMutableArray *deleteModelArray=[[NSMutableArray alloc]init];
    for (NSIndexPath *idexPath in array) {
        [deleteModelArray addObject:[self.modelArray_M objectAtIndex:idexPath.row]];
    }
    [self.modelArray_M removeObjectsInArray:deleteModelArray];
    [deleteModelArray removeAllObjects];
    [self.tableView reloadData];
}
@end

ZLShopModel.h
#import <Foundation/Foundation.h>

@interface ZLShopModel : NSObject
/** 已购买人数*/
@property(copy,nonatomic)NSString *buyCount;
/** 饭店配图*/
@property(copy,nonatomic)NSString *icon;
/** 价格*/
@property(copy,nonatomic)NSString *price;
/** 饭店名*/
@property(copy,nonatomic)NSString *title;
//字典转模型
+(instancetype)shopModelWithDictionary:(NSDictionary *)dict;
@end
ZLShopModel.m
#import "ZLShopModel.h"
@implementation ZLShopModel
+(instancetype)shopModelWithDictionary:(NSDictionary *)dict{
    ZLShopModel *shop=[[self alloc]init];
    //KVC。
    [shop setValuesForKeysWithDictionary:dict];
    return shop;
}
@end
ZLTableViewCell.h
#import <UIKit/UIKit.h>
#import "ZLShopModel.h"
@interface ZLTableViewCell : UITableViewCell
@property(strong,nonatomic)ZLShopModel *model;
//根据tableView,创建cell
+(instancetype)cellForTableView:(UITableView *)tableView;
@end
ZLTableViewCell.m
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "ZLTableViewCell.h"
#import <Masonry.h>

@interface ZLTableViewCell ()
@property(weak,nonatomic)UIImageView *iconImageView;
@property(weak,nonatomic)UILabel *contentLabel;
@property(weak,nonatomic)UILabel *priceLabel;
@property(weak,nonatomic)UILabel *buyCountLabel;
@end
@implementation ZLTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIImageView *iconImageView=[[UIImageView alloc]init];
        [self.contentView addSubview:iconImageView];
        self.iconImageView=iconImageView;
        UILabel *contentLabel=[[UILabel alloc]init];
        [self.contentView addSubview:contentLabel];
        self.contentLabel=contentLabel;
        UILabel *priceLabel=[[UILabel alloc]init];
        [self.contentView addSubview:priceLabel];
        self.priceLabel=priceLabel;
        UILabel *buyCountLabel=[[UILabel alloc]init];
        [self.contentView addSubview:buyCountLabel];
        buyCountLabel.textColor=[UIColor lightGrayColor];
        buyCountLabel.textAlignment=NSTextAlignmentRight;
        self.buyCountLabel=buyCountLabel;
        
        CGFloat offsetNumber=10.f;
        __weak typeof(self)weakSelf=self;
        [iconImageView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentView).offset(offsetNumber);
            make.left.equalTo(weakSelf.contentView).offset(offsetNumber);
            make.bottom.equalTo(weakSelf.contentView).offset(-offsetNumber);
            make.width.equalTo(weakSelf.frame.size.width/3);
        }];
        [contentLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(iconImageView);
            make.left.equalTo(iconImageView.right).offset(offsetNumber);
            make.right.equalTo(weakSelf.contentView).offset(-offsetNumber);
            make.height.equalTo(offsetNumber*4);
        }];
        [priceLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(contentLabel.bottom);
            make.left.equalTo(iconImageView.right).offset(offsetNumber);
            make.bottom.equalTo(weakSelf.contentView).offset(-offsetNumber);
            make.width.equalTo(weakSelf.frame.size.width/3);
        }];
        [buyCountLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(contentLabel.bottom);
            make.left.equalTo(priceLabel.right).offset(offsetNumber);
            make.bottom.equalTo(weakSelf.contentView).offset(-offsetNumber);
            make.right.equalTo(weakSelf.contentView).offset(-offsetNumber);
        }];
    }
![Uploading 屏幕快照 2016-06-12 下午5.31.08_954588.png . . .]
    return self;
}
//重写本类model属性,在model有值的那一刻给cell的各个属性进行赋值
-(void)setModel:(ZLShopModel *)model{
    self.iconImageView.image=[UIImage imageNamed:model.icon];
    self.contentLabel.text=model.title;
    self.priceLabel.text=[NSString stringWithFormat:@"¥ %@",model.price];
    self.buyCountLabel.text=[NSString stringWithFormat:@"%@人已购买",model.buyCount];
}
//封闭cell的创建过程,自定义cell,cell的事务应该放在cell里处理
+(instancetype)cellForTableView:(UITableView *)tableView{
    static NSString *identifier=@"cell";
    ZLTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell=[[ZLTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    return cell;
}
@end

竖屏效果图 横屏效果图
上一篇下一篇

猜你喜欢

热点阅读