// // topicListCell.m // ThePaperHD // // Created by scar1900 on 15/4/23. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "topicListCell.h" #import "AsyncImageView.h" #import "UIImage+wiRoundedRectImage.h" #import "SDWebImageManager.h" @interface topicListCell() { CGFloat timeWidth; CGFloat praiseWidth; CGFloat statusWidth; NSString *statusStr; CGFloat contentHeight; } @property(nonatomic, strong)AsyncImageView *backImgView; @property(nonatomic, strong)UIImageView *backView; @property(nonatomic, strong)UIImageView *headPicImgView; @property(nonatomic, strong)UIImageView *smallIconView; @property(nonatomic, strong)UILabel *userNameLabel; @property(nonatomic, strong)UILabel *contentLabel; @property(nonatomic, strong)UILabel *categoryLabel; @property(nonatomic, strong)UILabel *publishTimeLabel; @property(nonatomic, strong)UIImageView *praiseImgView; @property(nonatomic, strong)UILabel *praiseNumLabel; @property(nonatomic, strong)UILabel *statusLabel; @property(nonatomic, strong)UILabel *isTopLabel; @property(nonatomic, strong)UIImageView *isTopImg; @end @implementation topicListCell @synthesize topicBO = _topicBO; - (void)prepareForReuse { [super prepareForReuse]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:self.backImgView]; [self.contentView addSubview:self.backView]; [self.contentView addSubview:self.headPicImgView]; [self.contentView addSubview:self.smallIconView]; [self.contentView addSubview:self.userNameLabel]; [self.contentView addSubview:self.contentLabel]; [self.contentView addSubview:self.categoryLabel]; // [self.contentView addSubview:self.publishTimeLabel];//【需求】话题卡片视觉调整(bug:4341) [self.contentView addSubview:self.praiseImgView]; [self.contentView addSubview:self.praiseNumLabel]; [self.contentView addSubview:self.statusLabel]; [self.contentView addSubview:self.isTopLabel]; [self.contentView addSubview:self.isTopImg]; if ([[TPUserDefault instance].isNightMode intValue] > 0) { self.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; self.layer.borderWidth = 1; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAfterOpenNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } return self; } - (void)setTopicBO:(TopicInfoBO *)data { _topicBO = data; if (isBlankString(_topicBO.cornerLabel)) { self.isTopLabel.text = _topicBO.cornerLabel; self.isTopLabel.hidden = YES; self.isTopImg.hidden = YES; }else{ self.isTopLabel.hidden = NO; self.isTopImg.hidden = NO; if ([_topicBO.cornerLabel intValue] ==1) { self.isTopLabel.text = @"置顶"; }else if([_topicBO.cornerLabel intValue] ==2){ self.isTopLabel.text = @"热门"; }else if([_topicBO.cornerLabel intValue] == 3){ self.isTopLabel.text = @"最新"; } } if(!isBlankString(data.pic)){ //修复闪退:pic字段可能为空(bug fixed:4587) self.backImgView.imageUrl = data.pic; } self.headPicImgView.image = Image(@"setting/loginIcon_s.png"); // self.headPicImgView.imageUrl = data.userInfo[@"pic"]; userBO *user = setJsonDicToDataModel(data.userInfo, [userBO class]); //修复闪退:pic字段可能为空(bug fixed:4587) if (isBlankString(user.pic)) { self.headPicImgView.image = Image(@"setting/loginIcon_s.png"); }else { SDWebImageManager *imageManager = [SDWebImageManager sharedManager]; [imageManager downloadImageWithURL:[NSURL URLWithString:user.pic] options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *img, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ UIImage *roundImage = [UIImage createRoundedRectImage:img size:CGSizeMake(35, 35) radius:35/2]; dispatch_async(dispatch_get_main_queue(), ^{ self.headPicImgView.image = roundImage; }); }); }]; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ timeWidth = widthForString(data.publishTime, appFont(11, NO), 13, NSLineBreakByWordWrapping); praiseWidth = widthForString(data.praiseTimes, appFont(11, NO), 13, NSLineBreakByWordWrapping); statusStr = @""; if ([data.status intValue] == 0) { statusStr = @"待审核"; }else if ([data.status intValue] == 1) { if ([data.replyedNum integerValue] <= 0) {//【需求】话题:增加提问征集中的状态(bug:4323) statusStr = @"提问征集中"; }else{ statusStr = @"进行中..."; } }else if ([data.status intValue] == 2) { statusStr = @"审核不通过"; }else if ([data.status intValue] == 3) { statusStr = @"已关闭提问"; } contentHeight = heightForString(data.title, self.contentLabel.font, CGRectGetWidth(self.contentView.bounds)-40, self.contentLabel.lineBreakMode); statusWidth = widthForString(statusStr, appFont(11, NO), 13, NSLineBreakByWordWrapping); NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(readyToReLayout) object:nil]; [[TPUserDefault instance].globalQueue addOperation:operation]; }); } - (void)readyToReLayout { [self performSelectorOnMainThread:@selector(updateCellUI:) withObject:self.topicBO waitUntilDone:YES]; usleep(1); } - (void)updateCellUI:(TopicInfoBO*)data { self.userNameLabel.text = data.userInfo[@"sname"]; self.contentLabel.text = data.title; self.categoryLabel.text = data.categoryName; self.publishTimeLabel.text = data.publishTime; self.praiseNumLabel.text = data.praiseTimes; self.statusLabel.text = statusStr; if ([ data.userInfo[@"isAuth"] intValue] == 1) { self.smallIconView.hidden = NO; }else self.smallIconView.hidden = YES; // if ([data.isTop intValue] == 0) { // self.isTopLabel.hidden = YES; // }else self.isTopLabel.hidden = NO; [self layoutLabels]; } - (AsyncImageView*)backImgView { if (!_backImgView) { _backImgView = [[AsyncImageView alloc]initWithFrame:CGRectZero]; _backImgView.isHaveWaterPrint = NO; } return _backImgView; } - (UIImageView*)backView { if (!_backView) { _backView = [[UIImageView alloc]initWithFrame:CGRectZero]; // _backView.backgroundColor = [UIColor colorWithHexString:@"0x1b3053"]; // _backView.alpha = 0.4; _backView.image = Image(@"topic/topicListYaAn.png"); } return _backView; } - (UIImageView*)headPicImgView { if (!_headPicImgView) { _headPicImgView = [[UIImageView alloc]initWithFrame:CGRectZero]; _headPicImgView.backgroundColor = [UIColor clearColor]; _headPicImgView.image = Image(@"setting/loginIcon_s.png"); } return _headPicImgView; } - (UIImageView*)smallIconView { if (!_smallIconView) { _smallIconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)]; _smallIconView.hidden = YES; _smallIconView.image = Image(@"setting/vipIcon.png"); } return _smallIconView; } - (UILabel*)userNameLabel { if (!_userNameLabel) { _userNameLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _userNameLabel.textColor = [UIColor whiteColor]; _userNameLabel.font = appFont(11, NO); _userNameLabel.textAlignment = NSTextAlignmentLeft; _userNameLabel.backgroundColor = [UIColor clearColor]; } return _userNameLabel; } - (UILabel*)contentLabel { if (!_contentLabel) { _contentLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _contentLabel.textColor = [UIColor whiteColor]; _contentLabel.font = appFont(17, NO); _contentLabel.textAlignment = NSTextAlignmentLeft; _contentLabel.backgroundColor = [UIColor clearColor]; _contentLabel.lineBreakMode = NSLineBreakByWordWrapping; _contentLabel.numberOfLines = 0; } return _contentLabel; } - (UILabel*)categoryLabel { if (!_categoryLabel) { _categoryLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _categoryLabel.textColor = [UIColor whiteColor]; _categoryLabel.font = appFont(11, NO); _categoryLabel.textAlignment = NSTextAlignmentLeft; _categoryLabel.backgroundColor = [UIColor clearColor]; } return _categoryLabel; } //- (UILabel*)publishTimeLabel {//【需求】话题卡片视觉调整(bug:4341) // if (!_publishTimeLabel) { // _publishTimeLabel = [[UILabel alloc]initWithFrame:CGRectZero]; // _publishTimeLabel.textColor = [UIColor whiteColor]; // _publishTimeLabel.font = appFont(11, NO); // _publishTimeLabel.textAlignment = NSTextAlignmentCenter; // _publishTimeLabel.backgroundColor = [UIColor clearColor]; // } // return _publishTimeLabel; //} - (UIImageView*)praiseImgView { if (!_praiseImgView) { _praiseImgView = [[UIImageView alloc]initWithImage:Image(@"topic/topicPraiseIcon.png")]; _praiseImgView.userInteractionEnabled = YES; } return _praiseImgView; } - (UILabel*)praiseNumLabel { if (!_praiseNumLabel) { _praiseNumLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _praiseNumLabel.textColor = [UIColor whiteColor]; _praiseNumLabel.font = appFont(11, NO); _praiseNumLabel.textAlignment = NSTextAlignmentLeft; _praiseNumLabel.backgroundColor = [UIColor clearColor]; } return _praiseNumLabel; } - (UILabel*)statusLabel { if (!_statusLabel) { _statusLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _statusLabel.textColor = [UIColor whiteColor]; _statusLabel.font = appFont(11, NO); _statusLabel.textAlignment = NSTextAlignmentLeft; _statusLabel.backgroundColor = [UIColor clearColor]; } return _statusLabel; } - (UILabel*)isTopLabel { if (!_isTopLabel) { _isTopLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _isTopLabel.textColor = [UIColor whiteColor]; _isTopLabel.font = appFont(11, NO); _isTopLabel.textAlignment = NSTextAlignmentCenter; _isTopLabel.backgroundColor = [UIColor clearColor]; // _isTopLabel.layer.borderColor = [UIColor whiteColor].CGColor; // _isTopLabel.layer.cornerRadius = 3; // _isTopLabel.layer.borderWidth = 1; //bug // _isTopLabel.text = @"置顶"; } return _isTopLabel; } -(UIImageView *)isTopImg{ if (!_isTopImg) { _isTopImg = [UIImageView new]; _isTopImg.image = Image(@"topic/topCorner.png"); _isTopImg.backgroundColor = [UIColor clearColor]; _isTopImg.hidden = YES; } return _isTopImg; } - (void)layoutLabels { // self.publishTimeLabel.frame = CGRectMake(CGRectGetMaxX(self.categoryLabel.frame)+13, // CGRectGetMinY(self.categoryLabel.frame), // timeWidth, // 13); self.contentLabel.frame = CGRectMake(CGRectGetMinX(self.headPicImgView.frame), CGRectGetHeight(self.contentView.bounds)-27-20-contentHeight, CGRectGetWidth(self.contentView.bounds)-40, contentHeight+5); self.praiseImgView.frame = CGRectMake(CGRectGetMaxX(self.categoryLabel.frame)+13, CGRectGetMinY(self.categoryLabel.frame), 21/2, 22/2); self.praiseNumLabel.frame = CGRectMake(CGRectGetMaxX(self.praiseImgView.frame)+4, CGRectGetMinY(self.categoryLabel.frame), praiseWidth, 13); self.statusLabel.frame = CGRectMake(CGRectGetMaxX(self.praiseNumLabel.frame)+13, CGRectGetMinY(self.categoryLabel.frame), statusWidth, 13); } - (void)layoutSubviews { [super layoutSubviews]; self.backImgView.frame = self.contentView.bounds; self.backView.frame = self.contentView.bounds; self.headPicImgView.frame = CGRectMake(20, 20, 35, 35); self.smallIconView.center = CGPointMake(self.headPicImgView.center.x+15, self.headPicImgView.center.y+12); self.userNameLabel.frame = CGRectMake(CGRectGetMaxX(self.headPicImgView.frame)+15, CGRectGetMinY(self.headPicImgView.frame), CGRectGetWidth(self.contentView.bounds)-15-35-20, 35); //CGRectGetHeight(self.contentView.bounds)-28-35-20 // self.contentLabel.frame = CGRectMake(CGRectGetMinX(self.headPicImgView.frame), // CGRectGetMaxY(self.headPicImgView.frame), // CGRectGetWidth(self.contentView.bounds)-40, // CGRectGetHeight(self.contentView.bounds)-27-contentHeight); self.contentLabel.frame = CGRectMake(CGRectGetMinX(self.headPicImgView.frame), CGRectGetHeight(self.contentView.bounds)-27-20-contentHeight, CGRectGetWidth(self.contentView.bounds)-40, contentHeight+5); self.categoryLabel.frame = CGRectMake(CGRectGetMinX(self.headPicImgView.frame), CGRectGetHeight(self.contentView.bounds)-27, 23, 13); // self.publishTimeLabel.frame = CGRectMake(CGRectGetMaxX(self.categoryLabel.frame)+13, // CGRectGetMinY(self.categoryLabel.frame), // timeWidth, // 13); self.praiseImgView.frame = CGRectMake(CGRectGetMaxX(self.categoryLabel.frame)+13, CGRectGetMinY(self.categoryLabel.frame), 21/2, 22/2); self.praiseNumLabel.frame = CGRectMake(CGRectGetMaxX(self.praiseImgView.frame)+4, CGRectGetMinY(self.categoryLabel.frame), praiseWidth, 13); self.statusLabel.frame = CGRectMake(CGRectGetMaxX(self.praiseNumLabel.frame)+13, CGRectGetMinY(self.categoryLabel.frame), statusWidth, 13); self.isTopLabel.frame = CGRectMake(CGRectGetWidth(self.contentView.bounds)-44, CGRectGetMinY(self.categoryLabel.frame), 30, 14); self.isTopImg.frame = self.isTopLabel.frame; } - (void)refreshAfterOpenNightMode:(NSNotification*)noti { if ([[TPUserDefault instance].isNightMode intValue] > 0) { self.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; self.layer.borderWidth = 1; }else { self.layer.borderWidth = 0; } } - (void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self]; } @end