|
//
// myCreateController.m
// ThePaperBase
//
// Created by YoungLee on 15/8/26.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "myCreateController.h"
#import "topicModel.h"
#import "myCreateCell.h"
#import "InterViewPageViewController.h"
#import "topicContentController.h"
#import "creatTopicController.h"
#define DELCELLTAG 100001
@interface myCreateController ()<topicModelDelegate,myCreateCellDelegate>{
NSMutableArray *heightArray;
BOOL isFirstInto;
TopicInfoBO *deleTopic;
NSIndexPath *delIndexPath;
}
@property(nonatomic, strong)topicModel *model;
@property(nonatomic, strong)NSMutableArray *dataList;
@property(nonatomic, strong)UIView *noDataView;
@end
@implementation myCreateController
@synthesize dataList = _dataList;
@synthesize isRequest = _isRequest;
static NSString *myCreateCellID = @"myCreateCell";
- (id)init {
self = [super init];
if (self) {
isFirstInto = YES;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
[self.view addSubview:self.noDataView];
[self configCanRefresh:YES canLoad:YES];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView registerClass:[myCreateCell class] forCellReuseIdentifier:myCreateCellID];
self.model = topicModel.new;
self.model.delegate = self;
// [self firstAutoRefreshHandler];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshPageAfterCreat:) name:REFRESHAFTERCREATTOPIC object:nil];
// Do any additional setup after loading the view.
}
- (void)refreshPageAfterCreat:(NSNotification*)noti {
[self manualRefresh];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self manualRefresh];
}
-(void)pullLoadMoreHander{
if (!self.nextUrl) {
// TPLOG(@"数据有问题,无法获取新数据");
[self loadFailed];
return;
}else{
self.model.myCreateNextUrl = self.nextUrl;
}
}
-(void)pullRefreshHandler{
self.model.myCreateDic = nil;
}
-(void)setIsRequest:(BOOL)isReq{
_isRequest = isReq;
if (isFirstInto) {
// [self firstAutoRefreshHandler];
[self manualRefresh];
isFirstInto = NO;
}
}
#pragma mark -- click top
- (void)scrollTableViewToTop {
[self.tableView setContentOffset:CGPointZero 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];
UILabel *label = [UILabel new];
[self.dataList enumerateObjectsUsingBlock:^(TopicInfoBO *topicBO, NSUInteger idx, BOOL *stop) {
CGFloat titleHeight;
NSString *str = [NSString stringWithFormat:@"说明:%@",topicBO.rejectReson];
label.text = str;
//标题与顶部的间距
titleHeight = 20;
//标题高度
titleHeight = titleHeight + heightForString(topicBO.title, appFont(TEXT_FOUR_LEVELSIZE, NO), rect_screen.size.width-40, NSLineBreakByWordWrapping);
//状态高度35
titleHeight = titleHeight+40;
if ([topicBO.status integerValue] ==2) {
UILabel *label = [UILabel new];
label = [UILabel new];
label.textColor = [UIColor colorWithHexString:TextBlack];
label.font = appFont(TEXT_SEVEN_LEVELSIZE, NO);
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
label.text = str;
titleHeight = titleHeight + [label sizeThatFits:CGSizeMake(rect_screen.size.width-62, 0)].height;
[heightArray addObject:@(titleHeight+13)];
label = nil;
}else{
[heightArray addObject:@(titleHeight)];
}
}];
[self.tableView reloadData];
}
}
#pragma mark -- button hander
-(void)goCreate:(UIButton*)btn {
creatTopicController *createVC = [creatTopicController new];
[self.navigationController pushViewController:createVC 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(goCreate:) 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 {
myCreateCell *cell = [tableView dequeueReusableCellWithIdentifier:myCreateCellID];
if (nil == cell) {
cell = [[myCreateCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCreateCellID];
}
cell.topicInfo = _dataList[indexPath.row];
cell.myCreateDelegate = self;
cell.indexPath = indexPath;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
TopicInfoBO *topic = self.dataList[indexPath.row];
if ([topic.status intValue] ==0 || [topic.status intValue] == 2) {
creatTopicController *createVC = [creatTopicController new];
createVC.topicBO = topic;
createVC.tag = @"3";
[self.navigationController pushViewController:createVC animated:YES];
}else{
topicContentController *vc = [topicContentController new];
vc.preTopicInfo = self.dataList[indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
}
#pragma mark -- cell delegate
-(void)edit:(TopicInfoBO *)topicInfo indexPath:(NSIndexPath *)indexPath{
creatTopicController *createVC = [creatTopicController new];
createVC.topicBO = topicInfo;
createVC.tag = @"3";
[self.navigationController pushViewController:createVC animated:YES];
}
-(void)dele:(TopicInfoBO *)topicInfo indexPath:(NSIndexPath *)indexPath{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否删除您创建的话题?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
deleTopic = topicInfo;
delIndexPath = indexPath;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
NSDictionary *dict = @{@"topicId":deleTopic.topicId,@"otype":@"1"};
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:DELCELLTAG requestUrl:optTopicURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[Self.tableView beginUpdates];
[Self.dataList removeObjectAtIndex:delIndexPath.row];
[heightArray removeObjectAtIndex:delIndexPath.row];
[Self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:delIndexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
[Self.tableView endUpdates];
if(Self.dataList.count <=0){
[Self manualRefresh];
}
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
ShowTextMessage(message);
});
}
}];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark -- model delegate
-(void)returnMyCreateList:(NSMutableArray *)data url:(NSString *)url{
self.dataList = data;
self.nextUrl = url;
[self endRefresh];
[self.tableView setContentOffset:CGPointMake(0, 1)];
}
-(void)returnMYCreateNextUrl:(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
|