|
//
// myCreateController.m
// ThePaperHD
//
// Created by liyuan on 15/4/21.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "myCreateController.h"
#import "myCreateCell.h"
#import "otherPersonHomepageController.h"
#import "topicModel.h"
@interface myCreateController ()<myCreateCellDelegate,topicModelDelegate> {
BOOL needRefresh;
}
@property(nonatomic, strong)topicModel *model;
@end
@implementation myCreateController
@synthesize myCreateList;
@synthesize heightArray;
- (void)viewDidLoad {
[super viewDidLoad];
self.topic = @"创建";
needRefresh = NO;
[self.view addSubview:self.noDataBack];
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
self.tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
self.tableView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
[self configCanRefresh:YES canLoad:YES];
self.tableView.refreshControl.originalContentInsectY = 0;
[self manualRefresh];
self.model = [topicModel new];
self.model.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshPageAfterCreat:) name:REFRESHAFTERCREATTOPIC object:nil];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tableView.editing = YES;
[self.tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (needRefresh) {
[self manualRefresh];
ShowTextMessageWithDelay(@"提交成功,审核通过后将在问吧中显示",3);
needRefresh = NO;
}
}
- (BOOL)checkIfNoData:(NSArray*)list {
if (list.count > 0) {
self.tableView.hidden =NO;
self.noDataBack.hidden = YES;
heightArray = [NSMutableArray array];
CGFloat titleHeight;
for (int i =0 ; i < myCreateList.count; i++) {
TopicInfoBO *topicBO = myCreateList[i];
//话题:我的关注话题列表中未读数及红点标志位置不统一,要求统一格式显示(bug:4288)将40改成25
CGFloat labelWidth = widthForString(topicBO.title, appFont(18, NO), 25, NSLineBreakByWordWrapping);
if(0 ==[topicBO.status integerValue] || 2==[topicBO.status integerValue]){
if (labelWidth <= gambitCenterPopSize.width-67) {
titleHeight = 25;
}else{
titleHeight = heightForString(topicBO.title, appFont(18, NO), gambitCenterPopSize.width-67, NSLineBreakByWordWrapping);
}
}else{
if (labelWidth <= gambitCenterPopSize.width-30) {
titleHeight = 25;
}else{
titleHeight = heightForString(topicBO.title, appFont(18, NO), gambitCenterPopSize.width-30, NSLineBreakByWordWrapping);
}
}
if ([topicBO.status integerValue] ==2) {
CGFloat errorHeight = heightForString(topicBO.rejectReson, appFont(9, NO), gambitCenterPopSize.width-30, NSLineBreakByWordWrapping);
[heightArray addObject:@(titleHeight+67+errorHeight)];
}else{
[heightArray addObject:@(titleHeight+41)];
}
}
[self.tableView reloadData];
return NO;
}else {
self.tableView.hidden =YES;
self.noDataBack.hidden =NO;
return YES;
}
}
#pragma mark - refresh and load handler
- (void)pullRefreshHandler {
self.model.myCreateDic = nil;
// [Remote doJsonAction:1
// requestUrl:myTopicListURL
// parameter:nil
// delegate:self];
}
- (void)pullLoadMoreHander {
if (!self.nextUrl) {
TPLOG(@"数据有问题,无法获取新数据");
[self loadSuccess];
return;
}
self.model.myCreateNextUrl = self.nextUrl;
// [Remote doJsonAction:2
// requestUrl:self.nextUrl
// parameter:nil
// delegate:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView*)noDataBack {
if (!_noDataBack) {
_noDataBack = [[UIView alloc]initWithFrame:CGRectMake(0,
0,
gambitCenterPopSize.width,
CGRectGetHeight(self.view.bounds))];
_noDataBack.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_noDataBack.hidden = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(90, 350/2, gambitCenterPopSize.width-180, 60)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHexString:TextGray];
label.font = appFont(20, NO);
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = @"还没有创建的话题哦,有什么想要和大家分享和探讨的,赶快行动吧!";
[_noDataBack addSubview:label];
TPCustomButton *gotoCreate = [[TPCustomButton alloc] initWithFrame:CGRectMake((gambitCenterPopSize.width-300)/2, CGRectGetMaxY(label.frame)+65, 300, 40)];
[gotoCreate setTitle:@"去创建" forState:UIControlStateNormal];
gotoCreate.titleLabel.textColor = [UIColor whiteColor];
gotoCreate.titleLabel.font = appFont(18, NO);
[gotoCreate setBackgroundColor:[UIColor colorWithHexString:BLUECOLOR]];
[gotoCreate addTarget:self action:@selector(goCreate:) forControlEvents:UIControlEventTouchUpInside];
[_noDataBack addSubview:gotoCreate];
}
return _noDataBack;
}
#pragma delegate --
-(void) goCreate:(UIButton *)btn{
if ([self.delegate respondsToSelector:@selector(gotoCreate:tag:)]) {
[self.delegate gotoCreate:nil tag:@"0"];//进入创建页面
}
}
-(void)edit:(TopicInfoBO *)topicInfo indexPath:(NSIndexPath *)indexPath{
if ([self.delegate respondsToSelector:@selector(gotoCreate:tag:)]) {
[self.delegate gotoCreate:topicInfo tag:@"1"];//进入修改页面
}
}
-(void)dele:(TopicInfoBO *)topicInfo indexPath:(NSIndexPath *)indexPath{
// TopicInfoBO *topicInfoBO = myCreateList[indexPath.row];
NSDictionary *dict = @{@"topicId":topicInfo.topicId,@"otype":@"1"};
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:3 requestUrl:optTopicURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[Self.tableView beginUpdates];
[myCreateList removeObjectAtIndex:indexPath.row];
[Self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
[heightArray removeObjectAtIndex:indexPath.row];
if(myCreateList.count <=0){
// self.tableView.hidden =YES;
// self.noDataBack.hidden =NO;
[self manualRefresh];
}
[Self.tableView endUpdates];
}else{
ShowTextMessage(message);
}
}];
}
-(void)setMyCreateList:(NSMutableArray *)list{
myCreateList = list;
[self checkIfNoData:myCreateList];
}
#pragma mark - table view delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return myCreateList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [heightArray[indexPath.row] floatValue];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIndetifier = @"myCreateCell";
myCreateCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
if (nil == cell) {
cell = [[myCreateCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIndetifier];
}
cell.myCreateDelegate = self;
// [cell setData:myCreateList[indexPath.row]];
cell.indexPath = indexPath;
cell.topicInfo = myCreateList[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - remote handler
-(void)returnMyCreateList:(NSMutableArray *)data url:(NSString *)url{
self.myCreateList = data;
self.nextUrl = url;
// [self.tableView reloadData];
[self endRefresh];
}
-(void)returnMYCreateNextUrl:(NSMutableArray *)data url:(NSString *)url{
NSMutableArray *oldArray = [NSMutableArray arrayWithArray:self.myCreateList];
[oldArray addObjectsFromArray:data];
self.myCreateList = oldArray;
self.nextUrl = url;
[self.tableView reloadData];
}
//- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
// if (actionTag == 1) {
// NSArray *topicTempList = responsData[@"topicList"];
// NSMutableArray *topicArray = [NSMutableArray array];
// [topicTempList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// TopicInfoBO *topicInfo = setJsonDicToDataModel(obj, [TopicInfoBO class]);
// topicInfo.desc = obj[@"description"];
// NSString *newNums = obj[@"newNums"];
// if (newNums) {
// topicInfo.unNums = newNums;
//// if([topicInfo.status integerValue] == 1){
//// topicInfo.unNums = newNums;
//// }else{
//// topicInfo.unNums = @"0";
//// }
// }
// [topicArray addObject:topicInfo];
// }];
// self.myCreateList = topicArray;
// NSString *url = responsData[@"nextUrl"];
// self.nextUrl = url;
// [self.tableView reloadData];
// [self endRefresh];
// }else {
// NSArray *topicTempList = responsData[@"topicList"];
// NSMutableArray *qaArray = [NSMutableArray arrayWithArray:myCreateList];
// NSMutableArray *newArray = [NSMutableArray array];
//// [myCreateList removeAllObjects];
// [topicTempList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
// TopicInfoBO *topicBo = setJsonDicToDataModel(obj, [TopicInfoBO class]);
// topicBo.desc = obj[@"description"];
// NSString *newNums = obj[@"newNums"];
// if (newNums) {
// topicBo.unNums = newNums;
// }
// [newArray addObject:topicBo];
// }];
// [qaArray addObjectsFromArray:newArray];
// self.myCreateList = qaArray;
//// myCreateList = [NSMutableArray arrayWithArray:qaArray];
// [self loadSuccess];
// NSString *url = responsData[@"nextUrl"];
// self.nextUrl = url;
// [self.tableView reloadData];
// }
//
//}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// TopicInfoBO *infoBo = myCreateList[indexPath.row];
// if ([infoBo.status isEqualToString:@"0"] || [infoBo.status isEqualToString:@"2"]) {
// return YES;
// }else{
// return NO;
// }
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
TopicInfoBO *topicInfoBO = myCreateList[indexPath.row];
NSDictionary *dict = @{@"topicId":topicInfoBO.topicId,@"otype":@"1"};
[Remote doJsonActionWithBlock:3 requestUrl:optTopicURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[self.tableView beginUpdates];
[myCreateList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[heightArray removeObjectAtIndex:indexPath.row];
if(myCreateList.count <=0){
// self.tableView.hidden =YES;
// self.noDataBack.hidden =NO;
[self manualRefresh];
}
[self.tableView endUpdates];
}else{
ShowTextMessage(message);
}
}];
}
}
#pragma mark -- delegate
-(void)gotoTopicInfo:(TopicInfoBO *)topicInfo indexPath:(NSIndexPath *)indexPath{
if ([self.delegate respondsToSelector:@selector(gotoCreateTopic:)]) {
[self.delegate gotoCreateTopic:topicInfo];
TopicInfoBO *topic = myCreateList[indexPath.row];
topic.unNums = @"0";
[myCreateList replaceObjectAtIndex:indexPath.row withObject:topic];
[self.tableView reloadData];
}
}
- (void)gotoCreat:(TopicInfoBO *)topicInfo indexPath:(NSIndexPath *)indexPath{
if ([self.delegate respondsToSelector:@selector(gotoCreate:tag:)]) {
[self.delegate gotoCreate:topicInfo tag:@"0"];
TopicInfoBO *topic = myCreateList[indexPath.row];
topic.unNums = @"0";
[myCreateList replaceObjectAtIndex:indexPath.row withObject:topic];
[self.tableView reloadData];
}
}
- (void)refreshPageAfterCreat:(NSNotification*)noti {
needRefresh = YES;
}
#pragma mark - dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
|