当前段和后天同时开发时,造假数据是对于开发者来说是一件很家常便饭的事,写下此例子供大家分享,不多说直接上代码。
本实例是用UITableView显示,使用MVC编程模式开发
首先创建Model
// KTVModel.h
#import
@interface KTVModel : NSObject
@property(nonatomic,strong) NSString *ktvTitleName;
@property(nonatomic,strong) NSString *ktvImageName;
@property(nonatomic,strong) NSString *ktvContent;
@property(nonatomic,strong) NSString *ktvlPrice;
@property(nonatomic,strong) NSMutableArray *ktvDa;
@end
// KTVModel.m
#import "KTVModel.h"
@implementation KTVModel
@end
其次创建View
// KTVCell.h
#import
#import "KTVModel.h"
@interface KTVCell :UITableViewCell
@property (nonatomic,strong) UIImageView *shopImage;
@property (nonatomic,strong) UILabel *shopNameLabel;
@property (nonatomic,strong) UILabel *shopInfoLabel;
@property (nonatomic,strong) UILabel *priceLabel;
@property (nonatomic,strong) UILabel *soldedLabel;
@property (nonatomic,strong) KTVModel *KtvData;
@end
// KTVCell.m
#import "KTVCell.h"
#import "Public.h"
@implementation KTVCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
[selfinitViews];
}
returnself;
}
#pragma mark - 初始化控件创建类方法(封装控件)
-(void)initViews{
//图片
self.shopImage = [[UIImageViewalloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
self.shopImage.layer.cornerRadius = 4.0;
self.shopImage.layer.masksToBounds = YES;
[self.contentViewaddSubview:self.shopImage];
//免预约
UIImageView *yuyueImgView = [[UIImageViewalloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
[yuyueImgView setImage:[UIImageimageNamed:@"ic_deal_noBooking"]];
[self.contentViewaddSubview:yuyueImgView];
//商店店名
self.shopNameLabel = [[UILabelalloc] initWithFrame:CGRectMake(100, 5,screen_width-100-80, 30)];
[self.contentViewaddSubview:self.shopNameLabel];
//价格
self.priceLabel = [[UILabelalloc] initWithFrame:CGRectMake(100, 80, 100, 20)];
self.priceLabel.textColor =navigationBarColor;
[self.contentViewaddSubview:self.priceLabel];
//商店详细介绍
self.shopInfoLabel = [[UILabelalloc] initWithFrame:CGRectMake(100, 30,screen_width-100-10, 45)];
self.shopInfoLabel.textColor = [UIColorlightGrayColor];
self.shopInfoLabel.font = [UIFontsystemFontOfSize:13];
self.shopInfoLabel.numberOfLines = 2;
self.shopInfoLabel.lineBreakMode =NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
[self.contentViewaddSubview:self.shopInfoLabel];
}
- (void)awakeFromNib {
[superawakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selected animated:animated];
// Configure the view for the selected state
}
#pragma mark - 数据解析
-(void)setKtvData:(KTVModel *)KtvData{
_KtvData = KtvData;
self.shopImage.image = [UIImageimageNamed:_KtvData.ktvImageName];
NSLog(@"照片名字:%@",_KtvData.ktvImageName);
self.shopNameLabel.text =_KtvData.ktvTitleName;
NSLog(@"商店名字:%@",_KtvData.ktvTitleName);
self.shopInfoLabel.text = [NSStringstringWithFormat:@"%@",_KtvData.ktvContent];
NSLog(@"商店详情:%@",_KtvData.ktvContent);
NSString *priceStr = [NSStringstringWithFormat:@"¥%@起",_KtvData.ktvlPrice];
self.priceLabel.text = priceStr;
NSLog(@"商品价格:%@",priceStr);
}
@end
// KTVViewController.h
#import
@interface KTVViewController : UIViewController
@property(nonatomic,strong)UITableView *KTVTableView;
@end
// KTVViewController.m
#import "KTVViewController.h"
#import "Public.h"
#import "KTVCell.h"
#import "MJExtension.h"
@interface KTVViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSMutableArray *_KTVArray;
}
@end
@implementation KTVViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
[self.navigationControllersetNavigationBarHidden:YESanimated:YES];
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
[selfinitDataBase];
[selfinitTableView];
[selfgetRecommendData];
[selfsetNav];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 初始化数据
-(void)initDataBase{
_KTVArray = [[NSMutableArrayalloc] init];
}
#pragma mark - 初始化导航栏
-(void)setNav{
//导航栏背景
UIView *backView = [[UIViewalloc] initWithFrame:CGRectMake(0, 0,screen_width, 74)];
backView.backgroundColor =navigationBarColor;
[self.viewaddSubview:backView];
//设置返回按钮
UIButton *backBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
backBtn.frame =CGRectMake(10, 30, 23, 23);
[backBtn setImage:[UIImageimageNamed:@"back"]forState:UIControlStateNormal];
[backBtn addTarget:selfaction:@selector(OnBackBtn:)forControlEvents:UIControlEventTouchUpInside];
[backView addSubview:backBtn];
//设置界面标题
UILabel *navLable = [[UILabelalloc] initWithFrame:CGRectMake(screen_width/3, 32, screen_width/3, 25)];
navLable.text =@"KTV";
navLable.textAlignment =NSTextAlignmentCenter;
navLable.textColor = [UIColorwhiteColor];
navLable.adjustsFontSizeToFitWidth =YES;
navLable.highlighted =YES;
[backView addSubview:navLable];
}
#pragma mark - 设置UITableView
-(void)initTableView{
self.KTVTableView = [[UITableViewalloc] initWithFrame:CGRectMake(0, 64,screen_width,screen_heigth)style:UITableViewStyleGrouped];
self.KTVTableView.delegate=self;
self.KTVTableView.dataSource =self;
[self.viewaddSubview:self.KTVTableView];
}
#pragma mark - 获取推荐数据,此处造的假数据,可以在此方法下写网络请求数据,其中mj_objectWithKeyValues这个方法是第三方类库MJExtension可以pod'MJExtension'引入即可。
-(void)getRecommendData{
//造假数据记得删除
NSDictionary *recommendDict =@{
@"ktvDa": @[
@{
@"ktvTitleName" :@"鼎红K馆",
@"ktvImageName" :@"hotel6.jpg",
@"ktvContent": @"中包3个小时38元,大包3个小时98元,下午场欢唱券+A/B套餐美食任你选",
@"ktvlPrice" :@"38"
},
@{
@"ktvTitleName" :@"歌迷量贩KTV",
@"ktvImageName" :@"hotel5.jpg",
@"ktvContent":@"小包4小时55元,中包4小时55元,下午场送欢唱券",
@"ktvlPrice" :@"55"
},
@{
@"ktvTitleName" :@"歌歌量贩KTV",
@"ktvImageName" :@"hotel2.jpg",
@"ktvContent": @"中包6个小时42元,大包6个小时98元,下午场欢唱券+A/B套餐美食任你选",
@"ktvlPrice" :@"58"
},
@{
@"ktvTitleName" :@"糖果KTV",
@"ktvImageName" :@"hotel13.jpg",
@"ktvContent":@"阳光场/下午场5个小时59任你选",
@"ktvlPrice" :@"59"
},
@{
@"ktvTitleName" :@"纯K Party量贩KTV",
@"ktvImageName" :@"hotel10.jpg",
@"ktvContent": @"中包3个小时69元,大包3个小时108元,下午场欢唱券+A/B套餐美食任你选",
@"ktvlPrice" :@"69"
},
@{
@"ktvTitleName" :@"麦都量贩KTV",
@"ktvImageName" :@"hotel1.jpg",
@"ktvContent":@"迷你包7小时69,小包6个小时69",
@"ktvlPrice" :@"69"
},
@{
@"ktvTitleName" :@"珍钻量贩KTV",
@"ktvImageName" :@"hotel11.jpg",
@"ktvContent": @"中包6个小时42元,大包6个小时98元,下午场欢唱券+A/B套餐美食任你选",
@"ktvlPrice" :@"78"
},
@{
@"ktvTitleName" :@"美美时尚主题KTV",
@"ktvImageName" :@"hotel12.jpg",
@"ktvContent":@"阳光场/下午场5个小时89任你选",
@"ktvlPrice" :@"89"
}
]
};
//弱引用
__weak__typeof(self) weakself =self;
NSMutableArray *dataDic = [recommendDictobjectForKey:@"ktvDa"];
[_KTVArrayremoveAllObjects];
for (int i = 0; i < dataDic.count; i++) {
KTVModel *move = [KTVModelmj_objectWithKeyValues:dataDic[i]];
[_KTVArrayaddObject:move];
}
[weakself.KTVTableViewreloadData];
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return_KTVArray.count+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 0){
return 35.0;
}else{
return 100.0;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 5.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 5.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIViewalloc] initWithFrame:CGRectMake(0, 0,screen_width, 10)];
headerView.backgroundColor =RGB(239, 239, 244);
return headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIViewalloc] initWithFrame:CGRectMake(0, 0,screen_width, 0)];
footerView.backgroundColor =RGB(239, 239, 244);
return footerView;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 推荐数据
if(indexPath.row == 0){
staticNSString *cellIndentifier =@"morecell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIndentifier];
if (cell ==nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];
}
cell.textLabel.text =@"酒店推荐";
cell.selectionStyle =UITableViewCellSelectionStyleNone;
return cell;
}else{
staticNSString *cellIndentifier =@"recommendcell";
KTVCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIndentifier];
if (cell ==nil) {
cell = [[KTVCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];
}
if(_KTVArray.count!=0){
KTVModel *ktv =_KTVArray[indexPath.row-1];
[cell setKtvData:ktv];
}
cell.selectionStyle =UITableViewCellSelectionStyleNone;
return cell;
}
}
# pragma mark - 返回按钮触发事件
-(void)OnBackBtn:(UIButton *)sender{
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
@end
便于大家借鉴,方便自己用的时候直接拿来用。