屏幕適配+懶加載+MVC+KVC+多選刪除

《[一個程序猿的秘密基地](http://www.reibang.com/collection/7b76c71b2d73?utm_campaign=maleskine&utm_content=collection&utm_medium=reader_share&utm_source=weibo)》專題

【本文章只為給初學(xué)者閱讀使用,大牛繞行】(@^_^@)

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數(shù)據(jù)可變數(shù)組*/
@property(strong,nonatomic)NSMutableArray *modelArray_M;
@end

@implementation ViewController

/** 懶加載獲取plist文件的數(shù)據(jù)胚嘲,并把數(shù)據(jù)轉(zhuǎn)換成模型存入數(shù)組*/ //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:@"進入編輯狀態(tài)" 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;
}

/** 此方法用來創(chuàng)建頂部的按鈕-->
 *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
/** 已購買人數(shù)*/
@property(copy,nonatomic)NSString *buyCount;
/** 飯店配圖*/
@property(copy,nonatomic)NSString *icon;
/** 價格*/
@property(copy,nonatomic)NSString *price;
/** 飯店名*/
@property(copy,nonatomic)NSString *title;
//字典轉(zhuǎn)模型
+(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;
//根據(jù)tableView缸血,創(chuàng)建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的創(chuàng)建過程,自定義cell岩调,cell的事務(wù)應(yīng)該放在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

豎屏效果圖
橫屏效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末炒俱,一起剝皮案震驚了整個濱河市盐肃,隨后出現(xiàn)的幾起案子爪膊,更是在濱河造成了極大的恐慌,老刑警劉巖砸王,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件推盛,死亡現(xiàn)場離奇詭異,居然都是意外死亡谦铃,警方通過查閱死者的電腦和手機耘成,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來驹闰,“玉大人瘪菌,你說我怎么就攤上這事∴诶剩” “怎么了师妙?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵,是天一觀的道長屹培。 經(jīng)常有香客問我默穴,道長,這世上最難降的妖魔是什么褪秀? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任蓄诽,我火速辦了婚禮,結(jié)果婚禮上媒吗,老公的妹妹穿的比我還像新娘仑氛。我一直安慰自己,他們只是感情好闸英,可當(dāng)我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布调衰。 她就那樣靜靜地躺著,像睡著了一般自阱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上米酬,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天沛豌,我揣著相機與錄音,去河邊找鬼赃额。 笑死加派,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的跳芳。 我是一名探鬼主播芍锦,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼飞盆!你這毒婦竟也來了娄琉?” 一聲冷哼從身側(cè)響起次乓,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎孽水,沒想到半個月后票腰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡女气,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年杏慰,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片炼鞠。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡缘滥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谒主,到底是詐尸還是另有隱情朝扼,我是刑警寧澤,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布瘩将,位于F島的核電站吟税,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏姿现。R本人自食惡果不足惜肠仪,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望备典。 院中可真熱鬧异旧,春花似錦、人聲如沸提佣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拌屏。三九已至潮针,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間倚喂,已是汗流浹背每篷。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留端圈,地道東北人焦读。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像舱权,于是被迫代替她去往敵國和親矗晃。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,332評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫宴倍、插件张症、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,124評論 4 61
  • 有些時候就是奇怪仓技,也不知道是什么觸動了自己的哪個神經(jīng)。莫名的情緒爆發(fā)吠冤,而且一發(fā)不可收拾浑彰。這個時候我才知道,我以為我...
    李木只閱讀 543評論 1 1
  • “神筆”王鐸 王羲之在書法領(lǐng)域的地位是無可動搖的,如果說誰的書法作品可以和他相提并論涯保,那應(yīng)該是莫大...
    小凡文化閱讀 1,147評論 1 7
  • 形式本身并不重要诉濒,重要的是內(nèi)涵;行為本身并不重要夕春,重要的是動機未荒。 謙虛是中華民族的傳統(tǒng)美德,但同樣是“謙虛”及志,動機...
    時間中的看客閱讀 509評論 0 0