AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#import "MyViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor orangeColor];
[self.window makeKeyAndVisible];
ViewController *VC = [[ViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:VC];
naVC.tabBarItem = [[UITabBarItem alloc]init];
MyViewController *myVC = [[MyViewController alloc]init];
UINavigationController *myNAVC = [[UINavigationController alloc]initWithRootViewController:myVC];
myVC.tabBarItem = [[UITabBarItem alloc]init];
UITabBarController *tabBC = [[UITabBarController alloc]init];
tabBC.viewControllers = @[naVC,myNAVC];
self.window.rootViewController = tabBC;
return YES;
}
Activity.h
#import <Foundation/Foundation.h>
@interface Activity : NSObject<NSCoding>
@property(nonatomic,copy)NSString *activityId;
@property(nonatomic,copy)NSString *title;
@end
Activity.m
#import "Activity.h"
@implementation Activity
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.activityId forKey:@"activityId"];
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.title = [aDecoder decodeObjectForKey:@"title"];
self.activityId = [aDecoder decodeObjectForKey:@"activityId"];
}
return self;
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
if ([key isEqualToString:@"id"]) {
self.activityId = value;
}
}
@end
Save.h
#import <Foundation/Foundation.h>
#import "Activity.h"
@interface Save : NSObject
/判断活动是否收藏过
+ (BOOL)isActivityInPlist:(Activity *)activity;
/把活动保存到本地
//+ (void)saveActivityInPlist:(Activity *)activity;
+ (void)saveActivityOrCancel:(Activity *)activity state:(BOOL)state;
///找到所有的收藏信息
+ (NSMutableArray *)saveDetail;
@end
Save.m
#import "Save.h"
@implementation Save
+ (BOOL)isActivityInPlist:(Activity *)activity{
NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"activity.plist"];
NSLog(@"%@",NSHomeDirectory());
///先反归档
NSArray *actArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
if (actArr == nil) {
return NO;
}else{
for (Activity *temp in actArr) {
if ([temp.activityId isEqualToString:activity.activityId]) {
return YES;
}
}
return NO;
}
}
//+ (void)saveActivityInPlist:(Activity *)activity{
// ///为了防止原来数据被覆盖掉,所以一般先把数据线进行反归档,把数据读出来之后,新数组加进去,在进行归档
// NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
// NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"activity.plist"];
// NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
// ///有两种情况,第一种数组为空,需要初始化一个数组
// if (arr == nil) {
// arr = [NSMutableArray array];
// }
// [arr addObject:activity];
// [NSKeyedArchiver archiveRootObject:arr toFile:docPath];
//}
+ (void)saveActivityOrCancel:(Activity *)activity state:(BOOL)state{
///根据传过来的BOOL,判断当前的操作
if (state == NO) {
///为了防止原来数据被覆盖掉,所以一般先把数据线进行反归档,把数据读出来之后,新数组加进去,在进行归档
NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"activity.plist"];
NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
///有两种情况,第一种数组为空,需要初始化一个数组
if (arr == nil) {
arr = [NSMutableArray array];
}
[arr addObject:activity];
[NSKeyedArchiver archiveRootObject:arr toFile:docPath];
}else{
///删除
NSString *sandPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
NSString *docPath = [sandPath stringByAppendingPathComponent:@"activity.plist"];
NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
for (Activity *act in arr) {
if ([act.activityId isEqualToString:activity.activityId]) {
[arr removeObject:act];
break;
}
}
[NSKeyedArchiver archiveRootObject:arr toFile:docPath];
}
}
+ (NSMutableArray *)saveDetail{
NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject];
NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"activity.plist"];
NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
return arr;
}
@end
ActivityDetailViewController.h
#import <UIKit/UIKit.h>
#import "Activity.h"
@interface ActivityDetailViewController : UIViewController
@property(nonatomic,retain)Activity *activity;
@end
ActivityDetailViewController.m
#import "ActivityDetailViewController.h"
#import "Save.h"
@interface ActivityDetailViewController ()
@property(nonatomic,assign)BOOL isSave;
@end
@implementation ActivityDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:1];
button.frame = CGRectMake(0, 0, 60, 40);
//
self.isSave = [Save isActivityInPlist:self.activity];
if (self.isSave) {
[button setTitle:@"已收藏" forState:0];
}else{
[button setTitle:@"收藏" forState:0];
}
[button addTarget:self action:@selector(click:) forControlEvents:1<<6];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
self.title = self.activity.title;
}
-(void)click:(UIButton *)button{
// if (self.isSave) {
// NSLog(@"取消收藏");
// }else{
// [Save saveActivityInPlist:self.activity];
// self.isSave = YES;
// [button setTitle:@"已收藏" forState:0];
//
// }
[Save saveActivityOrCancel:self.activity state:self.isSave];
if (self.isSave) {
[button setTitle:@"收藏" forState:0];
}else{
[button setTitle:@"已收藏" forState:0];
}
///对状态进行取反
self.isSave = !self.isSave;
}
ViewController.m
#import "ViewController.h"
#import "MBProgressHUD.h"
#import "Activity.h"
#import "ActivityDetailViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)MBProgressHUD *hud;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createTableView];
[self createData];
self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.hud.labelText = @"加载中...";
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
self.title = @"活动";
}
- (void)createData{
NSString *strURL = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
NSURL *url = [NSURL URLWithString:strURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSMutableArray *arr = dic[@"events"];
self.arr = [NSMutableArray array];
for (NSDictionary *temp in arr) {
Activity *activity = [[Activity alloc]init];
[activity setValuesForKeysWithDictionary:temp];
[self.arr addObject:activity];
}
self.hud.hidden = YES;
[self.tableView reloadData];
});
}];
[task resume];
}
- (void)createTableView{
self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:0];
[self.view addSubview:self.tableView];
self.tableView.dataSource = self;
self.tableView.delegate =self;
self.tableView.rowHeight = 60;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Activity *activity = self.arr[indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
cell.textLabel.text = activity.title;
cell.textLabel.font = [UIFont systemFontOfSize:20];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ActivityDetailViewController *activityVC = [[ActivityDetailViewController alloc]init];
[self.navigationController pushViewController:activityVC animated:YES];
activityVC.activity = self.arr[indexPath.row];
///点击改变当前选中的背景颜色
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor orangeColor];
}