热更新demo

followManagerController.m 18KB

    // // followManagerController.m // ThePaperDemo // // Created by scar1900 on 14-10-15. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "followManagerController.h" #import "followHeaderCell.h" #import "followCententCell.h" #import "Reachability.h" @interface followManagerController () <UITableViewDelegate, UITableViewDataSource> @property(nonatomic, strong)UIButton *editBtn; @property(nonatomic, strong)UIButton *realEditBtn; @property(nonatomic, strong)UITableView *tableView; @property(nonatomic, strong)UIView *noDataView; @property(nonatomic, strong)UILabel *noDataLabel; @property(nonatomic, strong)UIImageView *noDataImage; @property(nonatomic, strong)UIButton *deleteAllBtn; @property(nonatomic, strong)NSMutableArray *dataList; @end @implementation followManagerController - (void)viewDidLoad { [super viewDidLoad]; [MobClick event:@"19"]; // Do any additional setup after loading the view. self.titleStr = @"我的跟踪"; self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode) name:REFRESHAFTERNIGHTMODE object:nil]; [self.naviBar addSubview:self.editBtn]; [self.naviBar addSubview:self.realEditBtn]; [self.view addSubview:self.tableView]; [self.view addSubview:self.noDataView]; [self.noDataView addSubview:self.noDataLabel]; [self.noDataView addSubview:self.noDataImage]; [self.view addSubview:self.deleteAllBtn]; [self layoutViews]; if (!self.dataList || self.dataList.count == 0) { [self remoteMethod]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; } - (void)needrefreshNightMode { //bug5058: 夜间模式实时切换 self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _editBtn.layer.borderColor = [UIColor colorWithHexString:BLUECOLOR].CGColor; [_editBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateNormal]; [_editBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted]; _noDataLabel.textColor = [UIColor colorWithHexString:TextGray]; [_deleteAllBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted]; _deleteAllBtn.backgroundColor = [UIColor colorWithHexString:BLUECOLOR]; } - (void)layoutViews { [self.editBtn makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.right).offset(-10); make.bottom.equalTo(self.naviBar.bottom).offset(-13); make.width.mas_equalTo(@30); make.height.mas_equalTo(@15); }]; [self.realEditBtn makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.right); make.bottom.equalTo(self.naviBar.bottom); make.width.mas_equalTo(@50); make.height.mas_equalTo(@40); }]; [self.noDataView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(self.barHeight+75, 0, 0, 0)); }]; [self.noDataLabel makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.noDataView.top); make.centerX.equalTo(self.noDataView.centerX); make.width.mas_equalTo(@270); make.height.mas_equalTo(@80); }]; [self.noDataImage makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.noDataLabel.bottom).offset(-20); make.centerX.equalTo(self.noDataView.centerX); make.width.mas_equalTo(@159); make.height.mas_equalTo(@242); }]; [self.tableView makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.naviBar.bottom); make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.bottom.equalTo(self.view.bottom); }]; [self.deleteAllBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.bottom.equalTo(self.view.bottom); make.height.mas_equalTo(@40); }]; } #pragma mark - get and set method - (UIButton*)editBtn { if (!_editBtn) { _editBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _editBtn.layer.cornerRadius = 3.0f; _editBtn.layer.masksToBounds = YES; _editBtn.layer.borderWidth = 1.0f; _editBtn.layer.borderColor = [UIColor colorWithHexString:BLUECOLOR].CGColor; [_editBtn setTitle:@"编辑" forState:UIControlStateNormal]; [_editBtn setTitle:@"完成" forState:UIControlStateSelected]; [_editBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateNormal]; [_editBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted]; _editBtn.hidden = YES; _editBtn.userInteractionEnabled = NO; } _editBtn.titleLabel.font = appFont(TEXT_SEVEN_LEVELSIZE, NO); return _editBtn; } - (UIButton*)realEditBtn { if (!_realEditBtn) { _realEditBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _realEditBtn.backgroundColor = [UIColor clearColor]; [_realEditBtn addTarget:self action:@selector(editTable:) forControlEvents:UIControlEventTouchUpInside]; _realEditBtn.hidden = YES; } return _realEditBtn; } - (UITableView*)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.backgroundColor = [UIColor clearColor]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSelectionStyleNone; _tableView.hidden = YES; } return _tableView; } - (UIView*)noDataView { if (!_noDataView) { _noDataView = [[UIView alloc] init]; _noDataView.backgroundColor = [UIColor clearColor]; _noDataView.hidden = YES; } return _noDataView; } - (UILabel*)noDataLabel { if (!_noDataLabel) { _noDataLabel = [[UILabel alloc] init]; _noDataLabel.textColor = [UIColor colorWithHexString:TextGray]; _noDataLabel.font = appFont(TEXT_THREE_LEVELSIZE, NO); _noDataLabel.textAlignment = NSTextAlignmentLeft; _noDataLabel.lineBreakMode = NSLineBreakByWordWrapping; _noDataLabel.numberOfLines = 0; _noDataLabel.backgroundColor = [UIColor clearColor]; _noDataLabel.text = @"读文章时,点击下方(跟踪)按钮,\n这里将集纳事件最新发展,杜绝新\n闻烂尾。"; } return _noDataLabel; } - (UIImageView*)noDataImage { if (!_noDataImage) { _noDataImage = [[UIImageView alloc]initWithImage:Image(@"follow/noTracker.png")]; } return _noDataImage; } - (UIButton*)deleteAllBtn { if (!_deleteAllBtn) { _deleteAllBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _deleteAllBtn.layer.masksToBounds = YES; [_deleteAllBtn setTitle:@"全部删除" forState:UIControlStateNormal]; [_deleteAllBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_deleteAllBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted]; _deleteAllBtn.backgroundColor = [UIColor colorWithHexString:BLUECOLOR]; [_deleteAllBtn addTarget:self action:@selector(deleteAll:) forControlEvents:UIControlEventTouchUpInside]; _deleteAllBtn.hidden = YES; } _deleteAllBtn.titleLabel.font = appFont(TEXT_TWO_LEVELSIZE, NO); return _deleteAllBtn; } - (void)setDataList:(NSMutableArray *)list { _dataList = list; [self checkDataList]; [self.tableView reloadData]; } - (void)checkDataList { if (self.dataList.count > 0) { self.tableView.hidden =NO; self.noDataView.hidden = YES; self.editBtn.hidden = NO; self.realEditBtn.hidden = NO; } else { self.tableView.hidden =YES; self.noDataView.hidden =NO; self.editBtn.hidden = YES; self.realEditBtn.hidden = YES; self.deleteAllBtn.hidden = YES; } } #pragma mark - remote method - (void)remoteMethod { __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:0 requestUrl:myTrackListURL parameter:nil withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { NSArray *dataList = responseData[@"trackerList"]; NSMutableArray *trackerList = [NSMutableArray array]; [dataList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSMutableArray *tracker = [NSMutableArray array]; trackerInfoBO *trackerBO = setJsonDicToDataModel(obj, [trackerInfoBO class]); trackerBO.unNum = obj[@"newNum"]; trackerBO.isOpen = @"0"; [tracker addObject:trackerBO]; NSArray *contentList = trackerBO.contList; [contentList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger index, BOOL *stop1) { listContObjectVO *listBO = setJsonDicToDataModel(dic, [listContObjectVO class]); [tracker addObject:listBO]; }]; [trackerList addObject:tracker]; }]; Self.dataList = trackerList; }else { ShowTextMessage(message); } }]; } #pragma mark - btn event handler - (void)editTable:(UIButton*)btn { btn.selected = !btn.selected; if (btn.selected) { [self.tableView setEditing:YES animated:YES]; self.deleteAllBtn.hidden = NO; [self.tableView remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.naviBar.bottom); make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.bottom.equalTo(self.deleteAllBtn.top); }]; [_editBtn setTitle:@"完成" forState:UIControlStateNormal]; }else { [self.tableView setEditing:NO animated:YES]; self.deleteAllBtn.hidden = YES; [self.tableView remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.naviBar.bottom); make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.bottom.equalTo(self.view.bottom); }]; [_editBtn setTitle:@"编辑" forState:UIControlStateNormal]; } } - (void)deleteAll:(UIButton*)btn { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否删除全部跟踪?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil]; [alert show]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex != 0) { __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:0 requestUrl:cancelTrackURL parameter:nil withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { Self.dataList = [NSMutableArray array]; }else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ ShowTextMessage(message); }); } }]; } } #pragma mark - tableview delegate and datasource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataList?self.dataList.count:0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (!self.dataList || self.dataList.count == 0) { return 0; }else { NSArray *list = self.dataList[section]; return list.count; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (!self.dataList || self.dataList.count == 0) { return 0; }else { NSArray *list = self.dataList[indexPath.section]; trackerInfoBO *trackerBO = list[0]; if (indexPath.row == 0) { return 60; }else { if ([trackerBO.isOpen intValue] == 1) { return 80; }else { return 0; } } } } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { return [self getFollowHeaderCell:tableView cellForRowAtIndexPath:indexPath]; } else { return [self getFollowContentCell:tableView cellForRowAtIndexPath:indexPath]; } } - (followHeaderCell*)getFollowHeaderCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *followHeaderCellID = @"followHeaderCellID"; followHeaderCell *cell = (followHeaderCell*)[tableView dequeueReusableCellWithIdentifier:followHeaderCellID]; if (nil == cell) { cell = [[followHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:followHeaderCellID]; } NSArray *list = self.dataList[indexPath.section]; id data = list[indexPath.row]; cell.trackerBO = data; return cell; } - (followCententCell*)getFollowContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *followCententCellID = @"followCententCellID"; followCententCell *cell = (followCententCell*)[tableView dequeueReusableCellWithIdentifier:followCententCellID]; if (nil == cell) { cell = [[followCententCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:followCententCellID]; } NSArray *list = self.dataList[indexPath.section]; listContObjectVO* listBO = list[indexPath.row]; trackerInfoBO* trackerBO = list[0]; if ([trackerBO.isOpen intValue] == 1) { cell.listBO = listBO; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[UIApplication sharedApplication]beginIgnoringInteractionEvents]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSMutableArray *list = [NSMutableArray arrayWithArray:self.dataList[indexPath.section]]; id data = list[indexPath.row]; if ([data isKindOfClass:[trackerInfoBO class]]) { trackerInfoBO *trackerBO = data; if ([trackerBO.isOpen intValue] == 0) { trackerBO.isOpen = @"1"; }else { trackerBO.isOpen = @"0"; } if ([trackerBO.unNum intValue] > 0) { NSDictionary *dic = @{@"trackId":trackerBO.trackerId}; [Remote doJsonActionWithBlock:1 requestUrl:myTrackResultListURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) { }]; } trackerBO.unNum = @"0"; //[list replaceObjectAtIndex:indexPath.row withObject:trackerBO]; //[self.dataList replaceObjectAtIndex:indexPath.section withObject:list]; [self.tableView beginUpdates]; [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone]; [self.tableView endUpdates]; }else { pushContentWithListContentObject(self.navigationController, data); } [[UIApplication sharedApplication]endIgnoringInteractionEvents]; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { return YES; } else { return NO; } } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { NSArray *list = self.dataList[indexPath.section]; trackerInfoBO *trackerInfo = list[0]; NSDictionary *dic = @{@"trackIds":trackerInfo.trackerId}; __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:0 requestUrl:cancelTrackURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { [Self.dataList removeObjectAtIndex:indexPath.section]; [Self.tableView beginUpdates]; [Self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; [Self checkDataList]; }else { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ ShowTextMessage(message); }); } }]; } } #pragma mark - naviBar - (void)tapNaviBar:(id)sender { if (![self.tableView isHidden]) { [self.tableView setContentOffset:CGPointZero animated:YES]; return; } } - (void) reachabilityChanged:(NSNotification *)notification { [self.tableView reloadData]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (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