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

《[一个程序猿的秘密基地](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

竖屏效果图
横屏效果图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,617评论 4 59
  • 有些时候就是奇怪,也不知道是什么触动了自己的哪个神经。莫名的情绪爆发,而且一发不可收拾。这个时候我才知道,我以为我...
    李木只阅读 524评论 1 1
  • “神笔”王铎 王羲之在书法领域的地位是无可动摇的,如果说谁的书法作品可以和他相提并论,那应该是莫大...
    小凡文化阅读 1,059评论 1 6
  • 形式本身并不重要,重要的是内涵;行为本身并不重要,重要的是动机。 谦虚是中华民族的传统美德,但同样是“谦虚”,动机...
    时间中的看客阅读 440评论 0 0