//// 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" //關(guān)于我們@interface LeftMenuViewController (){
NSArray *_tableDatas;//表格數(shù)據(jù)
}
//頭像視圖
@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 ------- 控制器實(shí)例化 ----------
-(LoginViewController *)loginVC{
if (!_loginVC) {
_loginVC = [[LoginViewController alloc]init];
}
return _loginVC;
}
#pragma mark ------- 控件實(shí)例化 ----------
//頭像視圖
-(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;
}
//賬號標(biāo)簽
-(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;
}
//選項(xiàng)表格
-(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:? //關(guān)于我們
{
AboutUsViewController *usVC = [[AboutUsViewController alloc]initWithTag:BaseViewControllerTag_Left];
UINavigationController *usNav = [[UINavigationController alloc]initWithRootViewController:usVC];
usVC.navigationItem.title = @"關(guān)于我們";
[self presentViewController:usNav animated:YES completion:nil];
}
break;
case 3:? //退出登錄
{
//底層數(shù)據(jù)庫業(yè)務(wù)退出
[[UserDataBase shaerDataBase]logOut];
//更新菜單視圖UI
[self updateAllUI];
//發(fā)出一個退出登錄的通知
[[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];
//點(diǎn)擊不變色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark ------觸發(fā)事件 -----
//頭像點(diǎn)擊觸發(fā)方法
-(void)headImgDidHandle:(id)sender{
NSLog(@"點(diǎn)擊了頭像");
if ([[UserDataBase shaerDataBase]userHadLogin]) {
//有用戶登錄,進(jìn)入個人信息視圖
PersonViewController *personVC = [[PersonViewController alloc]initWithTag:BaseViewControllerTag_Left];
UINavigationController *personNav = [[UINavigationController alloc]initWithRootViewController:personVC];
personVC.navigationItem.title = @"個人信息";
[self presentViewController:personNav animated:YES completion:nil];
}
else
{
//沒有用戶登錄,進(jìn)入登錄視圖
[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 = @[@"個人信息",@"我的收藏",@"關(guān)于我們",@"退出登錄"];
[self.table reloadData];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self updateAllUI];
}
//? 更新所有控件的UI顯示
-(void)updateAllUI{
[self.table reloadData];
//沒有用戶登錄
if (![[UserDataBase shaerDataBase]userHadLogin]) {
//頭像顯示默認(rèn)圖片
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;
//頭像設(shè)置
if (u.headImg == nil) {
//頭像顯示默認(rèn)圖片
self.headImgView.image = [UIImage imageNamed:@"40"];
}
else
{//設(shè)置頭像 顯示頭像
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