|
//
// collectTableController.m
// ThePaperBase
//
// Created by Huixin on 15/8/30.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "collectTableController.h"
#import "collectContentCell.h"
#import "Reachability.h"
@interface collectTableController ()
@end
@implementation collectTableController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.clipsToBounds = YES;
[self configCanRefresh:NO canLoad:YES];
[self needNoFirstAutoRefresh];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
}
- (void)setDataList:(NSMutableArray *)list {
_dataList = list;
[self.tableView reloadData];
}
- (void)pullLoadMoreHander {
if (isBlankString(self.nextUrl)) {
[self loadFailed];
return;
}
[Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self];
}
#pragma mark - remote delegate
-(void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData{
if (actionTag == 2) {
NSMutableArray *oldCollectList= [NSMutableArray arrayWithArray:self.dataList];
NSMutableArray *newCollectList = [NSMutableArray array];
NSArray *list = responsData[@"dataList"];
[list enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
listContObjectVO *listBo = setJsonDicToDataModel(obj, [listContObjectVO class]);
[newCollectList addObject:listBo];
}];
[oldCollectList addObjectsFromArray:newCollectList];
self.dataList = oldCollectList;
[self loadSuccess];
NSString *url = responsData[@"nextUrl"];
self.nextUrl = url;
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code {
ShowTextMessage(message);
[self loadFailed];
}
#pragma mark - uitableView
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (!_dataList || _dataList.count == 0) {
return 0;
}else {
return 80;
}
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *collectCell = @"collectContentCell";
collectContentCell *cell = [tableView dequeueReusableCellWithIdentifier:collectCell];
if (nil == cell) {
cell = [[collectContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:collectCell];
}
listContObjectVO* listBO = _dataList[indexPath.row];
[cell setListBO:listBO];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[UIApplication sharedApplication]beginIgnoringInteractionEvents];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.delegate respondsToSelector:@selector(collectGoToContent:)]) {
listContObjectVO *data = _dataList[indexPath.row];
[self.delegate collectGoToContent:data];
}
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
/**
* bug:5588( 频道导航页:连续点击会连续跳出的几个菜单)
*/
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
id data = _dataList[indexPath.row];
if ([data isKindOfClass:[listContObjectVO class]]) {
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) {
if (self.stateFlag == 0) {
NSMutableArray *collectList = [TPUserDefault instance].localCollectArray;
if (collectList.count > indexPath.row) {
[collectList removeObjectAtIndex:indexPath.row];
[TPUserDefault instance].localCollectArray = collectList;
}
[self.dataList removeObjectAtIndex:indexPath.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
if (self.dataList.count == 0) {
if (isBlankString(self.nextUrl)) {
if ([self.delegate respondsToSelector:@selector(checkDataList)]) {
[self.delegate checkDataList];
}
}
else
[Remote doJsonAction:2 requestUrl:myFavouriteURL parameter:nil delegate:self];
}
}
else {
listContObjectVO *list = _dataList[indexPath.row];
NSDictionary *dic = @{@"cids":list.contId};
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:0 requestUrl:deleteFavoriteURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[Self.dataList removeObjectAtIndex:indexPath.row];
[Self.tableView beginUpdates];
[Self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[Self.tableView endUpdates];
if (Self.dataList.count == 0) {
if (isBlankString(Self.nextUrl)) {
if ([Self.delegate respondsToSelector:@selector(checkDataList)]) {
[Self.delegate checkDataList];
}
}
else
[Remote doJsonAction:2 requestUrl:myFavouriteURL parameter:nil delegate:Self];
}
}else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
ShowTextMessage(message);
});
}
}];
}
}
}
- (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
|