本文共 8124 字,大约阅读时间需要 27 分钟。
//// LCBusinessTableViewController.m// 口碑页面//// Copyright © 2016年 LongChuang. All rights reserved.//#import "LCBusinessTableViewController.h"#import "LCBusinessData.h"#import "LCMyBusinessTableViewCell.h"@interface LCBusinessTableViewController ()@end@implementation LCBusinessTableViewController{ NSArray *_lcBusinessData;}- (void)viewDidLoad { [super viewDidLoad]; _lcBusinessData = [self loadBusinessData]; // 清空分割线的内边距 self.tableView.separatorInset = UIEdgeInsetsZero; // 添加头部广告 [self setupTableHeaderView]; // 添加尾巴 [self setupTableFooterView];}- (void)setupTableHeaderView { UINib *nib = [UINib nibWithNibName:@"MyBusinessXib" bundle:nil]; UIView *headerView = [[nib instantiateWithOwner:nil options:nil] firstObject]; // xib的宽 CGFloat xibW = headerView.bounds.size.width; // xib的高 CGFloat xibH = headerView.bounds.size.height; // tableView的宽 CGFloat tableViewW = self.tableView.bounds.size.width; // xib的高 3 * tableView的宽 4 / xib的宽 2 得到比率缩放之后的headerView的真实高度 CGFloat headerRealHeight = xibH * tableViewW / xibW; headerView.frame = CGRectMake(0, 0, 0, headerRealHeight); // 设置tableView的头部视图 self.tableView.tableHeaderView = headerView;}- (void)setupTableFooterView { UIButton *loadMoreDataBtn = [[UIButton alloc] init]; [loadMoreDataBtn setTitle:@"加载更多" forState:UIControlStateNormal]; [loadMoreDataBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; [loadMoreDataBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; loadMoreDataBtn.frame = CGRectMake(0, 0, 0, 30); // 设置按钮中的字体颜色 loadMoreDataBtn.titleLabel.font = [UIFont systemFontOfSize:13]; // loadMoreDataBtn.backgroundColor = [UIColor lightGrayColor]; // 设置灰度 如果想要白色就传1 如果想要纯黑传0 loadMoreDataBtn.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1]; // 给按钮添加监听事件 [loadMoreDataBtn addTarget:self action:@selector(loadMoreDataBtnClick) forControlEvents:UIControlEventTouchUpInside]; // 设置tableView尾部视图 self.tableView.tableFooterView = loadMoreDataBtn;}- (void)loadMoreDataBtnClick { // 创建控制器 UIViewController *test = [[UIViewController alloc] init]; test.view.backgroundColor = [UIColor yellowColor]; // 跳转到test控制器 [self.navigationController pushViewController:test animated:YES];}// 返回有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _lcBusinessData.count;}// 每一组的每一行显示什么内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.创建cell LCMyBusinessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"business" forIndexPath:indexPath]; // 2.设置cell内部子控件的数据 cell.lcBusinessdata = _lcBusinessData[indexPath.row]; // 3.返回cell return cell;}-(NSArray *)loadBusinessData{ // 通过url读取字典数据 NSArray *arr = [NSArray arrayWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"business.plist" withExtension:nil]]; NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count]; for (NSDictionary *dict in arr ) { [arrM addObject:[LCBusinessData businessWithDict:dict]]; } return arrM.copy;}// 返回每一组的头部标题- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"热门推荐";}@end
//// LCBusinessData.h// 口碑页面//// Copyright © 2016年 LongChuang. All rights reserved.//#import#import "UIKit/UIkit.h"@interface LCBusinessData : NSObject//配图@property (nonatomic, copy) NSString *icon;// 店名@property (nonatomic, copy) NSString *name;// 优惠信息@property (nonatomic, copy) NSString *discount;// 人均消费@property (nonatomic, assign) CGFloat averagePrice;// 距离@property (nonatomic, assign) CGFloat distance;// 打折@property (nonatomic, assign) float offNum;// 评价@property (nonatomic, assign) CGFloat level;+(instancetype)businessWithDict:(NSDictionary *)dict;@end
//// LCBusinessData.m// 口碑页面//// Copyright © 2016年 LongChuang. All rights reserved.//#import "LCBusinessData.h"@implementation LCBusinessData+(instancetype)businessWithDict:(NSDictionary *)dict{ id obj = [[self alloc]init]; [obj setValuesForKeysWithDictionary:dict]; return obj;}@end
//// LCLevelView.h// 外卖星级显示//// Copyright © 2016年 LongChuang. All rights reserved.//#import/** * 应根据得到的数据自动生成不同图案的星星图片 */@interface LCLevelView : UIView// 对外预留接口,根据传入的数据,通过属性的set方法,自动识别该生成何种星星@property(nonatomic,assign)CGFloat level;@end
//// LCLevelView.m// 外卖星级显示//// Copyright © 2016年 LongChuang. All rights reserved.//#import "LCLevelView.h"@implementation LCLevelView-(void)setLevel:(CGFloat)level{ // 强转获取整数部分 NSInteger grade = (NSInteger)level; // 根据整数部分生成全星图片 for (NSInteger i = 0; i < grade; i++) { [self createStartImage:@"full_star" position:i]; } // 判断是否需要生成半颗星图片 if (level - grade) { [self createStartImage:@"half_star" position:grade++]; } // 生成空的星星 for (NSInteger i = grade; i < 5; i++) { [self createStartImage:@"empty_star" position:i]; }}-(void)createStartImage:(NSString *)imageName position:(NSInteger)position{ UIImageView *imageview = nil; // 当星级评分发生变化时,无需重新创建,直接更改原来5张的图片名称即可,修改名称放到判断条件外 if (self.subviews.count == 5) { imageview = self.subviews[position]; }else { // 设置坐标 imageview = [[UIImageView alloc]init]; CGRect startFrame = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height); imageview.frame = CGRectOffset(startFrame, position * self.bounds.size.height, 0); // 创建出的图片空间添加到自定义UIView [self addSubview:imageview]; } // 无论是第几次创建,都需要更换图片 imageview.image = [UIImage imageNamed:imageName];}@end
//// LCMyBusinessTableViewCell.h// 口碑页面//// 自定义cell的类// Copyright © 2016年 LongChuang. All rights reserved.//#import@class LCBusinessData;@interface LCMyBusinessTableViewCell : UITableViewCell// 定义模型数据,当接收数据的时候同过set方法设置模板上的数据@property(nonatomic,strong)LCBusinessData *lcBusinessdata;@end
//// LCMyBusinessTableViewCell.m// 口碑页面//// Created by Long on 16/8/11.// Copyright © 2016年 LongChuang. All rights reserved.//#import "LCMyBusinessTableViewCell.h"#import "LCBusinessData.h"#import "LCLevelView.h"@class LCLevelView;@interface LCMyBusinessTableViewCell()// 配图@property (weak, nonatomic) IBOutlet UIImageView *iconView;// 店名@property (weak, nonatomic) IBOutlet UILabel *nameLabel;// 评分@property (weak, nonatomic) IBOutlet UILabel *levelLabel;// 人均消费@property (weak, nonatomic) IBOutlet UILabel *averagePriceLabel;// 打折@property (weak, nonatomic) IBOutlet UILabel *offNumLabel;// 距离@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;// 优惠信息@property (weak, nonatomic) IBOutlet UILabel *discountLabel;// 减字label@property (weak, nonatomic) IBOutlet UILabel *jian;// 自定义星级@property(weak,nonatomic)IBOutlet LCLevelView *levelStar;@end@implementation LCMyBusinessTableViewCell-(void)setLcBusinessdata:(LCBusinessData *)lcBusinessdata{ _lcBusinessdata = lcBusinessdata; self.iconView.image = [UIImage imageNamed:lcBusinessdata.icon]; self.nameLabel.text = lcBusinessdata.name; self.levelLabel.text = @(lcBusinessdata.level).description; self.averagePriceLabel.text = [NSString stringWithFormat:@"人均消费%.1f元",lcBusinessdata.averagePrice]; // 如果是64位时CGFloat就是一个double 类型的数值在转换成字符串时可能会出现灵异事件,如果出现就用float去替换 self.offNumLabel.text = [NSString stringWithFormat:@"%@折", @(lcBusinessdata.offNum).description]; self.distanceLabel.text = [NSString stringWithFormat:@"距离%.fm", lcBusinessdata.distance]; self.discountLabel.text = lcBusinessdata.discount; // 自定义星星 self.levelStar.level = lcBusinessdata.level;}/* layoutSubviews在以下情况下会被调用: 1、init初始化不会触发layoutSubviews 但是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,也会触发 2、addSubview会触发layoutSubviews 3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化 4、滚动一个UIScrollView会触发layoutSubviews 5、旋转Screen会触发父UIView上的layoutSubviews事件 6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件 */- (void)layoutSubviews { [super layoutSubviews]; self.jian.backgroundColor = [UIColor orangeColor];}@end