添加PCH文件Starry.pch
在程序Build Settings 的Prefix Header 写上$(SRCROOT)/工程名称/Starry.pch
Starry.pch
#ifndef Starry_pch
#define Starry_pch
/*
常用头文件的导入
*/
#import "BaseViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "UIView+NewMethods.h"
#import "NSString+NewMethods.h"
#import "UserDataBase.h"
#import "NewsDataBase.h"
#import "UIImage+Tailor.h"
#import "QRCodeGenerator.h"
#import "URLService.h"
#define App_Delegate (AppDelegate *)[UIApplication sharedApplication].delegate
/*
系统版本号
*/
#define VERSION_10_0 [UIDevice currentDevice].systemVersion.doubleValue >= 10.0
#define VERSION_9_0 [UIDevice currentDevice].systemVersion.doubleValue >= 9.0
/*
用于适配的宏
*/
#define SCR_W [UIScreen mainScreen].bounds.size.width
#define SCR_H [UIScreen mainScreen].bounds.size.height
//适配x轴的宏
#define FIT_X(w) (SCR_W / 375. *(w))
//适配Y轴的宏
#define FIT_Y(h) (SCR_H / 667. *(h))
/*
当前app版本号的宏
*/
#define VERSION_CURRENT [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"]
//持久化当前版本号的key的宏
#define NOT_FIRST_LANUCH @"NotFirstLanuch"
/*
Doucuments目录的宏
*/
#define DOCUMENT_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
/*
注册通知
*/
//退出登录
#define LOGOUT_NOTIFICATION_NAME @"logoutNotification"
#endif /* Starry_pch */
LeftMenuViewController.h
#import
@interface LeftMenuViewController : UIViewController
@end
LeftMenuViewController.m
#import "LeftMenuViewController.h"
#import "LoginViewController.h"
#import "PersonalViewController.h"
#import "CollecionViewController.h"
#import "AboutUsViewController.h"
@interface LeftMenuViewController ()
{
NSArray *_tableDatas;//表格数据
}
//头像视图
@property(nonatomic,strong)UIImageView *headImgView;
//账号lable
@property(nonatomic,strong)UILabel *accountLable;
//表格
@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_Y(80))];
_headImgView.center = CGPointMake(SCR_W/4+FIT_X(50), FIT_Y(104));
_headImgView.clipsToBounds = YES;
_headImgView.layer.cornerRadius = FIT_X(40);
_headImgView.image = [UIImage imageNamed:@"40"];
_headImgView.contentMode = UIViewContentModeScaleAspectFill;
_headImgView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImgDidHandle:)];
[_headImgView addGestureRecognizer:tap];
}
return _headImgView;
}
//账号标签
- (UILabel *)accountLable{
if (!_accountLable) {
_accountLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, FIT_X(300), FIT_Y(30))];
_accountLable.center = CGPointMake(self.headImgView.center.x, self.headImgView.center.y + self.headImgView.frame.size.height / 2 +FIT_Y(15)+FIT_Y(20));
_accountLable.text = @"17744530000";
_accountLable.textAlignment = NSTextAlignmentCenter;
_accountLable.textColor = [UIColor whiteColor];
_accountLable.font = [UIFont systemFontOfSize:18.0];
}
return _accountLable;
}
//表格
- (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.accountLable.center.y + self.accountLable.frame.size.height / 2 + 100 +FIT_Y(60));
_table.dataSource = self;
_table.delegate = self;
_table.scrollEnabled = NO;
_table.backgroundColor = [UIColor clearColor];
}
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 sharedDataBase]userHadLogin]) {
CollecionViewController *collVC = [[CollecionViewController 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 sharedDataBase]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 sharedDataBase]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)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = _tableDatas[indexPath.row];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont systemFontOfSize:18.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark -触发事件
//头像点击触发事件
- (void)headImgDidHandle:(id)sender{
if ([[UserDataBase sharedDataBase]userHadLogin]) {
//有用户登录,进入个人信息视图
PersonalViewController *personVC = [[PersonalViewController 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.accountLable];
[self.view addSubview:self.table];
}
#pragma mark - ViewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_tableDatas = @[@"个人信息",@"我的收藏",@"关于我们",@"退出登录"];
[_table reloadData];
}
#pragma mark-私有方法
- (void)updateAllUI{
//刷新表格
[self.table reloadData];
//没有用户登录
if (![[UserDataBase sharedDataBase]userHadLogin]) {
//头像显示默认图片
self.headImgView.image = [UIImage imageNamed:@"40"];
//账号lable不显示
self.accountLable.hidden = YES;
}
else{
//获取登录人信息
User *u = [[UserDataBase sharedDataBase]getCurrentLoginUser];
//显示账号
self.accountLable.hidden = NO;
self.accountLable.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;
}
}
}
#pragma mark - viewWillAppear
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self updateAllUI];
}
- (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
PersonView.h
#import
//自定义一个协议
@protocol PersonViewDegegate
-(void)headImageButtonPress ; //点击头像按钮后触发的回调方法 ;
-(void)handleUpdateWithNewUser:(User *)newU; //点击导航条右上角的保存更新触发的回调方法
- (void)gotoQRCodeViewController;//进入我的二维码视图
@end
@interface PersonView : UIView
@property(nonatomic,weak)iddelegate ;
//保存按钮
- (void)setUpdateButtonItem;
//获取选中的图片
- (void)getPassImage:(UIImage *)image;
//刷新UI内容
- (void)updateAllUI;
@end
PersonView.m
#import "PersonView.h"
@interface PersonView()
{
NSArray *_tableData;
UIImage *_selImg;
}
@property (nonatomic,strong)UIButton *headImgBtn;
@property (nonatomic,strong)UITableView *table;
@property (nonatomic,strong)UIBarButtonItem *updateBtn;
@end
@implementation PersonView
#pragma mark - 触发事件
- (void)headImgBtnHandle:(id)sender{
if ([(id)self.delegate respondsToSelector:@selector(headImageButtonPress)]) {
[self.delegate headImageButtonPress];
}
}
//更新按钮
- (void)updateHandle:(id)sender{
if ([(id)self.delegate respondsToSelector:@selector(handleUpdateWithNewUser:)]) {
[self showIdicatorMBProgressHUD];
//获取cell数组
NSArray *cells = self.table.visibleCells;
UITableViewCell *nameCell = cells[1];
UITextField *nameTF = (UITextField *)nameCell.accessoryView;
NSString *name = nameTF.text;
UITableViewCell *ageCell = cells[2];
UITextField *ageTF = (UITextField *)ageCell.accessoryView;
int age = [ageTF.text intValue];
UITableViewCell *adrCell = cells[3];
UITextField *adrTF = (UITextField *)adrCell.accessoryView;
NSString *adr = adrTF.text;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
User *u = [[User alloc]init];
u.ID = [UserDataBase sharedDataBase].getCurrentLoginUser.ID;
u.phone = [UserDataBase sharedDataBase].getCurrentLoginUser.phone;
u.password = [UserDataBase sharedDataBase].getCurrentLoginUser.password;
if (_selImg != nil) {
//给头像图片命名
u.headImg = [NSString stringWithFormat:@"%@.jpg",[UserDataBase sharedDataBase].getCurrentLoginUser.phone];
//将图片存入沙盒中
NSString *filePath = [DOCUMENT_PATH stringByAppendingPathComponent:u.headImg];
//
[UIImageJPEGRepresentation(_selImg, 0.5)writeToFile:filePath atomically:YES];
// [UIImagePNGRepresentation([_selImg scaleToSize:CGSizeMake(size.width/10, size.height/10)]) writeToFile:filePath atomically:YES];
}else{
if ([UserDataBase sharedDataBase].getCurrentLoginUser.headImg != nil) {
u.headImg = [[UserDataBase sharedDataBase]getCurrentLoginUser].headImg;
}
}
u.name = name;
u.age = age;
u.address = adr;
[self.delegate handleUpdateWithNewUser:u];
});
}
}
#pragma mark - touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
[self endEditing:YES];
[self resumeUI];
}
#pragma mark -UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
[self resumeUI];
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField.tag == 103) {//地址
[UIView animateWithDuration:0.378 animations:^{
self.frame = CGRectMake(0, -20, SCR_W, SCR_H);
}];
}
}
//恢复
- (void)resumeUI{
[UIView animateWithDuration:0.378 animations:^{
self.frame = CGRectMake(0, 64, SCR_W, SCR_H-64);
}];
}
#pragma mark -UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == _tableData.count-1) {
[self.delegate gotoQRCodeViewController];
}
}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tableData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//设置标题栏
cell.textLabel.text = _tableData[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置cell的附件视图
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, FIT_X(150), FIT_Y(40))];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.tag = indexPath.row+100;
tf.delegate = self;
if (indexPath.row == 0) {
tf.text = [[UserDataBase sharedDataBase]getCurrentLoginUser].phone;
tf.enabled = NO;
}else if (indexPath.row == 1)
{
tf.text= [[UserDataBase sharedDataBase]getCurrentLoginUser].name;
}
else if (indexPath.row == 2) {
tf.keyboardType = UIKeyboardTypeNumberPad;
tf.text = [NSString stringWithFormat:@"%d",[[UserDataBase sharedDataBase]getCurrentLoginUser].age];
}
else if (indexPath.row == 3)
{
tf.text = [[UserDataBase sharedDataBase]getCurrentLoginUser].address;
}
//
if (indexPath.row != _tableData.count-1) {
cell.accessoryView = tf;
}
else{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
#pragma mark - 控件实例化
- (UIButton *)headImgBtn{
if (!_headImgBtn) {
_headImgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_headImgBtn.frame = CGRectMake(0, 0, 80, 80);
_headImgBtn.center = CGPointMake(SCR_W/2, FIT_Y(100));
[_headImgBtn setImage:[UIImage imageNamed:@"83.5"] forState:UIControlStateNormal];
_headImgBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
_headImgBtn.clipsToBounds = YES;
_headImgBtn.layer.cornerRadius = 40;
[_headImgBtn addTarget:self action:@selector(headImgBtnHandle:) forControlEvents:UIControlEventTouchUpInside];
}
return _headImgBtn;
}
- (UITableView *)table{
if (!_table) {
_table = [[UITableView alloc]initWithFrame:CGRectMake(FIT_X(50), FIT_Y(200), SCR_W - FIT_X(100), FIT_Y(50)*_tableData.count) style:UITableViewStylePlain];
_table.rowHeight = FIT_Y(50);
_table.dataSource = self;
_table.delegate = self;
_table.scrollEnabled = NO;
// _table.allowsSelection = NO;
}
return _table;
}
- (UIBarButtonItem *)updateBtn{
if (!_updateBtn) {
_updateBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存更新" style:UIBarButtonItemStylePlain target:self action:@selector(updateHandle:)];
[_updateBtn setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
}
return _updateBtn;
}
#pragma mark - init---
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self addSubview:self.headImgBtn];
_tableData = @[@"账号",@"姓名",@"年龄",@"地址",@"我的二维码"];
[self addSubview:self.table];
}
return self;
}
//保存按钮
- (void)setUpdateButtonItem{
[self getCurrentViewController].navigationItem.rightBarButtonItem = self.updateBtn;
}
//获取选中图片
-(void)getPassImage:(UIImage *)image{
_selImg = image;
[self.headImgBtn setImage:_selImg forState:UIControlStateNormal];
}
//刷新UI内容
- (void)updateAllUI{
[self.table reloadData];
User *u = [[UserDataBase sharedDataBase] getCurrentLoginUser];
if (u.headImg == nil) {
[self.headImgBtn setImage:[UIImage imageNamed:@"83.5"] forState:UIControlStateNormal];
}else{
//获取沙盒中存储的图片
NSString *filePath = [DOCUMENT_PATH stringByAppendingPathComponent:u.headImg];
UIImage *img = [[UIImage alloc]initWithContentsOfFile:filePath];
[self.headImgBtn setImage:img forState:UIControlStateNormal];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
PersonalViewController.h
#import
@interface PersonalViewController : BaseViewController
@end
PersonalViewController.m
#import "PersonalViewController.h"
#import "PersonView.h"
#import "MyQRCodeViewController.h"
@interface PersonalViewController ()
@property (nonatomic,strong)PersonView *personV;
@property (nonatomic,strong)UIImagePickerController *imgPickCtl;//图片拾取器
@property (nonatomic,strong)MyQRCodeViewController *qrVC;//我的二维码控制器
@end
@implementation PersonalViewController
#pragma mark -重写
- (PersonView *)personV{
if (!_personV) {
_personV = [[PersonView alloc]initWithFrame:self.view.frame];
_personV.delegate = self;
}
return _personV;
}
- (UIImagePickerController *)imgPickCtl{
if (!_imgPickCtl) {
_imgPickCtl = [[UIImagePickerController alloc]init];
_imgPickCtl.delegate = self;
_imgPickCtl.editing = YES;
}
return _imgPickCtl;
}
- (MyQRCodeViewController *)qrVC{
if (!_qrVC) {
_qrVC = [[MyQRCodeViewController alloc]init];
}
return _qrVC;
}
#pragma mark-UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *img = nil;
//判断是否编辑图片
if (picker.editing == NO) {
//获取图片
img = [info objectForKey:UIImagePickerControllerOriginalImage];
}else{
//获取编辑后的图片
img = [info objectForKey:UIImagePickerControllerEditedImage];
}
[self.personV getPassImage:img];
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark- PersonViewDegegate
//进入我的二维码视图
- (void)gotoQRCodeViewController{
self.qrVC.navigationItem.title = @"我的二维码";
self.qrVC.passPhone = [[UserDataBase sharedDataBase]getCurrentLoginUser].phone;
[self.navigationController pushViewController:self.qrVC animated:YES];
}
//执行更新
-(void)handleUpdateWithNewUser:(User *)newU{
BOOL success = [[UserDataBase sharedDataBase] updateUser:newU];
dispatch_async(dispatch_get_main_queue(), ^{
[self.personV hideIdicatorMBProgressHUD];
if (success) {
[self.personV showTextStyleMBProgressHUDWithTitle:@"更新成功"];
}else{
[self.personV showTextStyleMBProgressHUDWithTitle:@"更新失败"];
}
});
}
//选择头像
-(void)headImageButtonPress{
UIAlertController *alertCtl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//取消按钮
UIAlertAction *cancelAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//取消按钮点击后回调代码块
}];
//图片库
UIAlertAction *photoLibraryAct = [UIAlertAction actionWithTitle:@"照片库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//设置图片拾取器的来源
self.imgPickCtl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.imgPickCtl animated:YES completion:nil];
}];
//拍照
UIAlertAction *cameraAct = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//设置图片拾取器的来源
self.imgPickCtl.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imgPickCtl animated:YES completion:nil];
}
}];
[alertCtl addAction:cameraAct];
[alertCtl addAction:photoLibraryAct];
[alertCtl addAction:cancelAct];
[self presentViewController:alertCtl animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view = self.personV;
//保存按钮
[self.personV setUpdateButtonItem];
[self.personV updateAllUI];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
- (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
MyQRCodeViewController.h
#import "BaseViewController.h"
@interface MyQRCodeViewController : BaseViewController
@property (nonatomic,strong)NSString *passPhone;
@end
MyQRCodeViewController.m
#import "MyQRCodeViewController.h"
@interface MyQRCodeViewController ()
@property (nonatomic,strong)UIImageView *imgView;
@end
@implementation MyQRCodeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(FIT_X(100), FIT_Y(200), SCR_W-FIT_X(200), SCR_W- FIT_X(200))];
[self.view addSubview:self.imgView];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UIImage *img = [QRCodeGenerator qrImageForString:self.passPhone imageSize:SCR_W-FIT_X(200)];
self.imgView.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
CollecionViewController.h
#import
@interface CollecionViewController : BaseViewController
@end
CollecionViewController.m
#import "CollecionViewController.h"
#import "UIImageView+WebCache.h"
@interface CollecionViewController ()
{
NSMutableArray *_tableData;
}
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation CollecionViewController
#pragma mark - 实例化重写
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_tableData = [[NewsDataBase sharedDataBase]getUserSvedNews];
[self.tableView reloadData];
[self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -UITableViewDelegate
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
News *deletNew = _tableData[indexPath.row];
//1.删除底层数据
[[NewsDataBase sharedDataBase]deleteOneNewsByID:deletNew.ID];
//删除给表格赋值的数组中的对应数据
[_tableData removeObjectAtIndex:indexPath.row];
//2删除单元格
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//3.刷新表格
[tableView reloadData];
}
}
#pragma mark -UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tableData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
News *oneNew = _tableData[indexPath.row];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:oneNew.pic] placeholderImage:[UIImage imageNamed:@"40"]];
cell.textLabel.text = oneNew.title;
return cell;
}
/*
#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
AboutUsViewController.h
#import
@interface AboutUsViewController : BaseViewController
@end
AboutUsViewController.m
#import "AboutUsViewController.h"
#import "QRCodeScanViewController.h"
@interface AboutUsViewController ()
@property (nonatomic,strong)UILabel *resultLable;//显示回传结果标签
@end
@implementation AboutUsViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
scanBtn.frame = CGRectMake(0, 0, 30, 30);
[scanBtn setImage:[UIImage imageNamed:@"scan"] forState:UIControlStateNormal];
[scanBtn addTarget:self action:@selector(scanQRCodeImage:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:scanBtn];
self.resultLable = [[UILabel alloc]initWithFrame:CGRectMake(FIT_X(5), FIT_Y(100), SCR_W- FIT_X(10), FIT_Y(30))];
self.resultLable.layer.borderColor = [UIColor redColor].CGColor;
self.resultLable.layer.borderWidth = 1.0;
[self.view addSubview:self.resultLable];
}
#pragma mark - 触发方法
- (void)scanQRCodeImage:(id)sender{
QRCodeScanViewController *scanVC = [[QRCodeScanViewController alloc]init] ;
//扫描到二维码后回传的代码块
scanVC.Completion =^(id passString){
self.resultLable.text = passString;
[self.navigationController popToViewController:self animated:YES];
if ([passString isACorrectPhoneNumber]) {
UIAlertController *alertCtl = [UIAlertController alertControllerWithTitle:@"请选择" message:nil preferredStyle:UIAlertControllerStyleAlert];
//拨打电话
[alertCtl addAction:[UIAlertAction actionWithTitle:@"拨打电话" style:UIAlertViewStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//调用系统自带的通话,执行拨号
NSString *urlStr = [NSString stringWithFormat:@"tel://%@",passString];
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url];
}]];
//发送短信
[alertCtl addAction:[UIAlertAction actionWithTitle:@"发送短信" style:UIAlertViewStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//调用系统自带的通话,执行拨号
NSString *urlStr = [NSString stringWithFormat:@"sms://%@",passString];
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url];
}]];
//取消
[alertCtl addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertCtl animated:YES completion:nil];
}
else if ([passString isAURLString]){
NSURL *url = [NSURL URLWithString:passString];
[[UIApplication sharedApplication]openURL:url];
}
} ;
[self.navigationController pushViewController:scanVC animated:YES];
}
- (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