|
//
// myAttentionController.m
// ThePaperBase
//
// Created by YoungLee on 15/8/26.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "myAttentionController.h"
#import "topicModel.h"
#import "myAttentionCell.h"
#import "InterViewPageViewController.h"
#import "topicContentController.h"
#define DELETAG 10001
@interface myAttentionController ()<topicModelDelegate>{
NSMutableArray *heightArray;
}
@property(nonatomic, strong)topicModel *model;
@property(nonatomic, strong)NSMutableArray *dataList;
@property(nonatomic, strong)UIView *noDataView;
@property(nonatomic, strong)UIButton *selectBtn;
@property(nonatomic, strong)UIButton *editBtn;
@end
@implementation myAttentionController
@synthesize dataList = _dataList;
@synthesize isEdit = _isEdit;
static NSString *attenCellID = @"myAttentionCell";
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
[self.view addSubview:self.noDataView];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView registerClass:[myAttentionCell class] forCellReuseIdentifier:attenCellID];
[self configCanRefresh:YES canLoad:YES];
self.model = topicModel.new;
self.model.delegate = self;
[self firstAutoRefreshHandler];
// Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
[self manualRefresh];
[self.tableView setEditing:_isEdit animated:YES];
}
-(void)pullLoadMoreHander{
if (!self.nextUrl) {
// TPLOG(@"数据有问题,无法获取新数据");
[self loadFailed];
return;
}else{
self.model.myAttenNextUrl = self.nextUrl;
}
}
-(void)pullRefreshHandler{
self.model.myAttenDic = nil;
}
#pragma mark -- click top
- (void)scrollTableViewToTop {
[self.tableView setContentOffset:CGPointZero animated:YES];
}
-(void)setIsEdit:(BOOL)isOrEdit{
_isEdit = isOrEdit;
[self.tableView setEditing:_isEdit animated:YES];
}
#pragma mark -- setData
-(void)setDataList:(NSMutableArray *)list{
_dataList = list;
// [_dataList removeAllObjects];
if (_dataList.count<=0) {
self.tableView.hidden = YES;
self.noDataView.hidden = NO;
}else{
self.tableView.hidden = NO;
self.noDataView.hidden = YES;
heightArray = [NSMutableArray array];
[_dataList enumerateObjectsUsingBlock:^(TopicInfoBO *bo, NSUInteger idx, BOOL *stop) {
CGFloat titleHeight = heightForString(bo.title, appFont(TEXT_FOUR_LEVELSIZE, NO), rect_screen.size.width-40, NSLineBreakByWordWrapping);
[heightArray addObject:@(titleHeight + 75)];
}];
[self.tableView reloadData];
}
}
#pragma mark -- button hander
-(void)gotoAskSelector:(UIButton*)btn {
InterViewPageViewController *vc = [InterViewPageViewController new];
vc.initIndex = 0;
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -- nodata view
-(UIView *)noDataView{
if (!_noDataView) {
_noDataView = [[UIView alloc]initWithFrame:CGRectMake(0,
0,
rect_screen.size.width,
CGRectGetHeight(self.view.bounds))];
_noDataView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_noDataView.hidden = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, rect_screen.size.width-60, 45)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHexString:TextGray];
label.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
label.textAlignment = NSTextAlignmentLeft;
label.text = @"还没有关注的话题哦,快去问吧逛逛吧!";
label.numberOfLines = 2;
[_noDataView addSubview:label];
TPCustomButton *gotoAsk = [[TPCustomButton alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(label.frame)+65, rect_screen.size.width - 40, 40)];
[gotoAsk setTitle:@"去问吧" forState:UIControlStateNormal];
gotoAsk.titleLabel.textColor = [UIColor colorWithHexString:BackGroundColor];
gotoAsk.titleLabel.font = appFont(18, NO);
[gotoAsk setBackgroundColor:[UIColor colorWithHexString:BLUECOLOR]];
[gotoAsk addTarget:self action:@selector(gotoAskSelector:) forControlEvents:UIControlEventTouchUpInside];
[_noDataView addSubview:gotoAsk];
}
return _noDataView;
}
#pragma mark -- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [heightArray[indexPath.row] floatValue];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
myAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:attenCellID];
if (nil == cell) {
cell = [[myAttentionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:attenCellID];
}
cell.topicInfo = _dataList[indexPath.row];
cell.indexPath = indexPath;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
topicContentController *vc = [topicContentController new];
vc.preTopicInfo = _dataList[indexPath.row];
vc.currentIndex = @"1";
[self.navigationController pushViewController:vc animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return _isEdit;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
TopicInfoBO *topicInfoBO = self.dataList[indexPath.row];
NSDictionary *dict = @{@"commentId":topicInfoBO.topicId,@"ot":@"1"};
[Remote doJsonActionWithBlock:DELETAG requestUrl:cancelAttentionURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[self.tableView beginUpdates];
[self.dataList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[heightArray removeObjectAtIndex:indexPath.row];
[self.tableView endUpdates];
if (self.dataList.count <= 0) {
[self manualRefresh];
}
}else{
ShowTextMessage(message);
}
}];
}
}
#pragma mark -- model delegate
-(void)returnMyAttenList:(NSMutableArray *)data url:(NSString *)url{
self.dataList = data;
self.nextUrl = url;
[self endRefresh];
[self.tableView setContentOffset:CGPointMake(0, 1)];
}
-(void)returnMyAttenNextList:(NSMutableArray *)data url:(NSString *)url{
NSMutableArray *oldArray = [NSMutableArray arrayWithArray:self.dataList];
[oldArray addObjectsFromArray:data];
self.dataList = oldArray;
self.nextUrl = url;
}
-(void)returnNoData{
[self endRefresh];
}
@end
|