《[一個程序猿的秘密基地](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
豎屏效果圖
橫屏效果圖