// // SecondViewController.m // PersonalCenter // // Created by Arch on 2017/6/16. // Copyright © 2017年 mint_bin. All rights reserved. // #import "SecondViewController.h" #import "HomeSubModel.h" #import "HomeSubItemModel.h" #import "HomeTeacherCell.h" #import "MoveViewController.h" #import "BookTeacherDetailVC.h" static NSString *const SecondViewControllerTableVIewCellIdentifier = @"SecondViewControllerTableVIewCell"; @interface SecondViewController () @property (nonatomic, strong) NSMutableArray * dataSource; @property(nonatomic, strong) UITableView *tableView; @property(nonatomic, strong) NSIndexPath * IndexPath; @property(nonatomic, strong) HomeSubItemModel * model; @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = UIColorHex(0xF8F9FB); [self creatTableView]; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (void)creatTableView { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT - NAVH - 150)]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.rowHeight = 50; [self.view addSubview:_tableView]; } - (void)setDataArray:(NSArray *)array { [self.dataSource removeAllObjects]; for (NSDictionary *dict in array) { HomeSubItemModel * model = [HomeSubItemModel modelWithDictionary:dict]; [self.dataSource addObject:model]; } [self.tableView reloadData]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { WS(weakSelf); HomeSubItemModel *model = self.dataSource[indexPath.row]; HomeTeacherCell * cell = [HomeTeacherCell configCell:tableView indexPath:indexPath]; [cell setDataWithModel:model]; cell.ClickCollectBlock = ^(NSInteger Id) { if (model.IsCollect) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"真的取消收藏吗?" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSMutableDictionary *dic = [[NSMutableDictionary alloc]init]; [dic setValue:@(Id) forKey:@"CollectionDataId"]; /// 1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8 会议详情 14工作流审批 300 文件 400 会议纪要 [dic setValue:@(CollectModel_Teacher) forKey:@"CollectionType"]; [dic setValue:@(0) forKey:@"FolderId"]; [dic setValue:@(0) forKey:@"SourceUserId"]; [[HttpManager sharedHttpManager] PUTUrl:Host(API_CreateCollect) parameters:dic success:^(id _Nonnull responseObject) { model.IsCollect = NO; dispatch_async(dispatch_get_main_queue(), ^{ [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }); } failure:^(NSError * _Nonnull error) { // SHOWERROR([ZYCTool handerResultData:error]); }]; }]; UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:actionYes]; [alert addAction:actionNo]; [weakSelf presentViewController:alert animated:YES completion:^{ }]; }); }else{ weakSelf.model = model; weakSelf.IndexPath = indexPath; [weakSelf gotoCollectHander:Id]; } }; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { HomeSubItemModel * model = self.dataSource[indexPath.row]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; BookTeacherDetailVC * vc = [BookTeacherDetailVC initBookTeacherDetailVC]; vc.Id = model.Id; [self.navigationController pushViewController:vc animated:YES]; } - (void)gotoCollectHander:(NSInteger)Id{ MoveViewController *vc = [MoveViewController initMoveViewController]; vc.CollectionDataId = Id; vc.collectType = CollectHanderType_Collect; vc.CollectionType = CollectModel_Teacher; vc.ParentId = 0; vc.titleStr = @"我的收藏"; vc.TypeId = CreateCollectionType; vc.FolderIds = @[].mutableCopy; [self.navigationController pushViewController:vc animated:YES]; } - (void)collectSuccess { self.model.IsCollect = YES; [self.tableView reloadRowsAtIndexPaths:@[self.IndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } @end