//// LeftMenuViewController.m// StarrySky//// Created by RickyWei on 2017/10/30.// Copyright © 2017年 BW. All rights reserved.//#import "LeftMenuViewController.h"#import "LoginViewController.h"#import "PersonViewController.h" //个人信息控制器#import "CollectionViewController.h" //我的收藏#import "AboutUsViewController.h" //关于我们@interface LeftMenuViewController (){
NSArray *_tableDatas;//表格数据
}
//头像视图
@property(nonatomic,strong)UIImageView *headImgView;
//账号label
@property(nonatomic,strong)UILabel *accountLabel;
//表格
@property(nonatomic,strong)UITableView *table;
//登录视图控制器
@property(nonatomic,strong)LoginViewController *loginVC;
@end
@implementation LeftMenuViewController
#pragma mark ------- 控制器实例化 ----------
-(LoginViewController *)loginVC{
if (!_loginVC) {
_loginVC = [[LoginViewController alloc]init];
}
return _loginVC;
}
#pragma mark ------- 控件实例化 ----------
//头像视图
-(UIImageView *)headImgView{
if (!_headImgView) {
_headImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, FIT_X(80), FIT_X(80))];
_headImgView.center = CGPointMake(SCREEN_W/4+FIT_X(50), FIT_Y(104));
_headImgView.clipsToBounds = YES;
_headImgView.layer.cornerRadius = FIT_X(40);
_headImgView.image = [UIImage imageNamed:@"40"];
_headImgView.userInteractionEnabled = YES;
_headImgView.contentMode = UIViewContentModeScaleAspectFill;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImgDidHandle:)];
[_headImgView addGestureRecognizer:tap];
}
return _headImgView;
}
//账号标签
-(UILabel *)accountLabel{
if (!_accountLabel) {
_accountLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, FIT_X(300), FIT_Y(30))];
_accountLabel.center = CGPointMake(self.headImgView.center.x, self.headImgView.center.y + self.headImgView.frame.size.height / 2 + FIT_Y(15) + FIT_Y(20));
_accountLabel.textAlignment = NSTextAlignmentCenter;
_accountLabel.text = @"18911121441";
_accountLabel.textColor = [UIColor whiteColor];
_accountLabel.font = [UIFont systemFontOfSize:18.];
}
return _accountLabel;
}
//选项表格
-(UITableView *)table{
if (!_table) {
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, FIT_X(300), 200) style:UITableViewStylePlain];
_table.rowHeight = 50;
_table.center = CGPointMake(self.headImgView.center.x, self.accountLabel.center.y + self.accountLabel.frame.size.height / 2 + 100 + FIT_Y(20));
_table.scrollEnabled = NO;
_table.backgroundColor = [UIColor clearColor];
_table.dataSource = self;
_table.delegate = self;
}
return _table;
}
#pragma mark - --- UITableViewDelegate ----
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case 0: //个人信息
[self headImgDidHandle:nil];
break;
case 1: //我的收藏
{
if ([[UserDataBase shaerDataBase]userHadLogin]) {
CollectionViewController *collVC = [[CollectionViewController alloc]initWithTag:BaseViewControllerTag_Left];
UINavigationController *collNav = [[UINavigationController alloc]initWithRootViewController:collVC];
collVC.navigationItem.title = @"我的收藏";
[self presentViewController:collNav animated:YES completion:nil];
}
else
{
[self presentViewController:self.loginVC animated:YES completion:nil];
}
}
break;
case 2: //关于我们
{
AboutUsViewController *usVC = [[AboutUsViewController alloc]initWithTag:BaseViewControllerTag_Left];
UINavigationController *usNav = [[UINavigationController alloc]initWithRootViewController:usVC];
usVC.navigationItem.title = @"关于我们";
[self presentViewController:usNav animated:YES completion:nil];
}
break;
case 3: //退出登录
{
//底层数据库业务退出
[[UserDataBase shaerDataBase]logOut];
//更新菜单视图UI
[self updateAllUI];
//发出一个退出登录的通知
[[NSNotificationCenter defaultCenter] postNotificationName:LOGOUT_NOTIFICATION_NAME object:nil];
}
break;
default:
break;
}
}
#pragma mark ----UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([[UserDataBase shaerDataBase] userHadLogin]) {
return _tableDatas.count;
}
else
{
return _tableDatas.count - 1;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = _tableDatas[indexPath.row];
//点击不变色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark ------触发事件 -----
//头像点击触发方法
-(void)headImgDidHandle:(id)sender{
NSLog(@"点击了头像");
if ([[UserDataBase shaerDataBase]userHadLogin]) {
//有用户登录,进入个人信息视图
PersonViewController *personVC = [[PersonViewController alloc]initWithTag:BaseViewControllerTag_Left];
UINavigationController *personNav = [[UINavigationController alloc]initWithRootViewController:personVC];
personVC.navigationItem.title = @"个人信息";
[self presentViewController:personNav animated:YES completion:nil];
}
else
{
//没有用户登录,进入登录视图
[self presentViewController:self.loginVC animated:YES completion:nil];
}
}
#pragma mark -------- loadView -------
-(void)loadView{
[super loadView];
[self.view addSubview:self.headImgView];
[self.view addSubview:self.accountLabel];
[self.view addSubview:self.table];
}
#pragma mark -------- View Load /Appear -------
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_tableDatas = @[@"个人信息",@"我的收藏",@"关于我们",@"退出登录"];
[self.table reloadData];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self updateAllUI];
}
// 更新所有控件的UI显示
-(void)updateAllUI{
[self.table reloadData];
//没有用户登录
if (![[UserDataBase shaerDataBase]userHadLogin]) {
//头像显示默认图片
self.headImgView.image = [UIImage imageNamed:@"40"];
//账号label不显示
self.accountLabel.hidden = YES;
}
else
{
//有用户登录的情况下
//获取登录人信息
User *u = [[UserDataBase shaerDataBase]getCurrentLoginUser];
//显示账号
self.accountLabel.hidden = NO;
self.accountLabel.text = u.phone;
//头像设置
if (u.headImg == nil) {
//头像显示默认图片
self.headImgView.image = [UIImage imageNamed:@"40"];
}
else
{//设置头像 显示头像
UIImage *img = [[UIImage alloc]initWithContentsOfFile:[DOCUMENT_PATH stringByAppendingPathComponent:u.headImg]];
self.headImgView.image = img;
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end