#import "CommunityCell.h" #import "LoginViewController.h" //#import "MJPhotoBrowser.h" //#import "MJPhoto.h" #import "DiaryVC.h" //表示风格1的cell最多10张。竖着排列 #define imgCntMax 10 static CommunityCell *selfCell; @interface CommunityCell() { UILabel* lblHead; UILabel* lblContent; UILabel* lblTag; UILabel* lblAddr; NSMutableArray* btns; NSInteger imgCnt; NSArray *urls; /**头像按钮。点击该按钮后,将会跳转到该用户的学车日记 */ UIButton* prtBtn; } @property(nonatomic,strong)NSMutableArray* imgs; @property(assign)NSInteger style; @end @implementation CommunityCell @synthesize imgs; - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } #pragma mark - -(instancetype)initWithStyle:(UITableViewCellStyle)st reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:st reuseIdentifier:reuseIdentifier]; if (self) { selfCell = self; UILabel* label; label = [[UILabel alloc] init]; [self addSubview:label]; lblAddr = label; label = [[UILabel alloc] init]; label.numberOfLines = 0; [self addSubview:label]; lblContent = label; label = [[UILabel alloc] init]; [self addSubview:label]; lblHead = label; label = [[UILabel alloc] init]; [self addSubview:label]; lblTag = label; _acceptLabel = [[UILabel alloc] init]; _acceptLabel.textAlignment = NSTextAlignmentCenter; _acceptLabel.hidden = YES; [self addSubview:_acceptLabel]; NSInteger tag = 0; btns = [NSMutableArray array]; UIButton* btn; btn = [[UIButton alloc] init]; [btn.imageView setContentMode:UIViewContentModeScaleAspectFit]; [self addSubview:btn]; [btns addObject:btn]; [btn setTag:tag++]; btn = [[UIButton alloc] init]; [btn.imageView setContentMode:UIViewContentModeScaleAspectFit]; [self addSubview:btn]; [btns addObject:btn]; [btn setTag:tag++]; btn = [[UIButton alloc] init]; [self addSubview:btn]; [btn setTag:tag++]; prtBtn = btn; [btn target:self]; [lblHead borderColor:RGB_COLOR(229, 160, 161) width:1 cornorRadios:3]; lblHead.textColor = RGB_COLOR(219, 113, 116); lblHead.layer.borderColor =RGB_COLOR(229, 160, 161).CGColor; lblHead.backgroundColor = RGB_COLOR(251, 241, 241); [lblContent setTextColor:kTitleColor]; [lblContent setFont:[UIFont scaleSize:NormalFont]]; [lblHead setFont:[UIFont scaleSize:12]]; lblHead.textAlignment = NSTextAlignmentCenter; [lblTag setFont:[UIFont scaleSize:13]]; [lblTag setTextColor:defGreen]; lblTag.textAlignment = NSTextAlignmentCenter; lblTag.layer.borderColor = defGreen.CGColor; lblTag.layer.masksToBounds = YES; lblTag.layer.borderWidth = 1; lblTag.numberOfLines = 0; [lblAddr setFont:[UIFont scaleSize:13]]; [lblAddr setTextColor:contentTextColor]; [btns[0] addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [btns[1] addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [btns[0] setImage:[UIImage imageNamed:@"community0.png"] forState:UIControlStateNormal]; [btns[0] setImage:[UIImage imageNamed:@"community1.png"] forState:UIControlStateSelected]; [btns[1] setImage:[UIImage imageNamed:@"community2.png"] forState:UIControlStateNormal]; [lblHead setText:@"置顶"]; } return self; } +(CommunityCell*)cellForTableView:(UITableView*)tableView Style:(NSInteger)st { NSString* cellid; cellid= @"CommunityCellIdentifier"; CommunityCell* cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (!cell) { cell = [[CommunityCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid]; cell.style = st; cell.imgs = [NSMutableArray array]; //普通风格0的cell最多3张图,横着排列。风格1的cell最多10张。竖着排列 int cnt = 0; switch (st) { case 0: case 2: cnt = 3; break; case 1: cnt = imgCntMax; break; default: break; } for (int i =0; i%@",urls); int i =0; imgCnt = 0; for (NSString* obj in urls) { if ([obj isEqualToString:@""]) { break; } NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; //图片路径 总是变化 算了 这里做个判断 NSString *str = [obj stringByTrimmingCharactersInSet:whitespace]; if (str && ![str hasPrefix:@"http"]){ str = [imgPreFix stringByAppendingString:[obj stringByTrimmingCharactersInSet:whitespace]]; } if ( 0 == _style || 2 == _style ) { if (i < 3) { //dansonimg [imgs[i++] sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"btn.png"]]; } }else if( 1 == _style){ if (i < imgCntMax) { //dansonimg [imgs[i++] sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"btn.png"]]; } } imgCnt++; } NSString *str = model[@"HEADIMG"]; if (str && ![str hasPrefix:@"http"]){ str = [imgPreFix stringByAppendingString:str]; } if (!str) { str = @""; } [self.imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:randomPortrait()]; [self.textLabel setText:model[@"TI_CRUSERNAME"]]; //搞定 HH是24格式的时间 hh是12小时 NSString *timeString = model[@"TI_CRDATE"]; NSDateFormatter *formatter = [NSDateFormatter rq_defaultDateFormatter]; NSDate *timeDate = [formatter dateFromString:timeString]; NSDate *date = [NSDate date]; NSTimeInterval timeStamp= [date timeIntervalSinceDate:timeDate]; int time = timeStamp; NSString *viewDate = model[@"TI_CRDATE"]; if (time > 0) { int second = time%60; int min = time/60; int hour = min/60; min = min%60; int day = hour/24; hour = hour%24; if (day > 0) { viewDate = [NSString stringWithFormat:@"%d天前",day]; if (day > 29) { viewDate = @"一个月前"; } if (day > 59) { viewDate = [model[@"TI_CRDATE"] substringToIndex:10]; } } else { if (hour > 0) { viewDate = [NSString stringWithFormat:@"%d小时前",hour]; } else { if (min > 0) { viewDate = [NSString stringWithFormat:@"%d分钟前",min]; } else { if (second > 10) { viewDate = [NSString stringWithFormat:@"%d秒前",second]; } else { viewDate = @"刚刚"; } } } } } //NSLog(@"时间---->%@--------viewDate----->%@",model[@"TI_CRDATE"],viewDate); self.detailTextLabel.text = viewDate; lblHead.hidden = ! [model[@"ISTOP"] integerValue]; if ([model[@"ISTOP"] integerValue]) { [lblContent setText:[@" " stringByAppendingString: model[@"CONTENT"] ]]; }else{ [lblContent setText: model[@"CONTENT"] ]; } if ([model[@"ISPOINT"] intValue]) { [btns[0] setSelected:YES]; }else{ [btns[0] setSelected:NO]; } [btns[0] setTitle:model[@"POINTNUMFROM"] textColor:contentTextColor font:13 fotState:UIControlStateNormal]; [btns[1] setTitle:model[@"COMMENTNUM"] textColor:contentTextColor font:13 fotState:UIControlStateNormal]; str = model[@"GROUPNAME"]; if (!str || [str isEqualToString:@""]) { str = @"吐槽"; } [lblTag setText:str]; NSString *cityStr = model[@"CITY"]; if (!cityStr) { cityStr = @""; } NSString *jxmcStr = model[@"JXMC"]; if (!jxmcStr) { jxmcStr = @""; } [lblAddr setText:[NSString stringWithFormat:@"%@ %@",cityStr,jxmcStr]]; // if ( 1 == [model[@"TI_TYPE"] intValue]) { // lblTag.hidden = NO; // }else{ // lblTag.hidden = YES; // } } /**【置顶】2个字。可以在前面添加3个一字占位。然后再上面添加一个label; */ -(void)layoutSubviews { [super layoutSubviews]; //NSLog(@"%d",(int)_style); //TI_TYPE = 4 是学车问答 switch (_style) { case 0: //话题 [self layoutStyle0]; break; case 1: //话题详情 [self layoutStyle1]; break; case 2: [self layoutStyle2]; break; default: break; } } -(void)layoutStyle1 { CGFloat wid = self.frame.size.width; CGFloat x,y,w,h,bd; bd = 10; w = h = 50; x = y = bd; prtBtn.frame = self.imageView.frame = CGRectMake(x, y, w, h); self.imageView.layer.cornerRadius = w/2.0; x += bd+w; w = wid - x - bd; h /= 2.0; self.textLabel.frame =CGRectMake(x, y, w, h); y+= h; self.detailTextLabel.frame =CGRectMake(x, y, w, h); x = bd; w = wid - 2*bd; y += bd + h; h = [lblContent.text heightForWid:w Font:NormalFont]+bd; lblContent.frame =CGRectMake(x, y, w, h); lblHead.frame =CGRectMake(x+1, y+3, [lblHead.text sizeForFont:13].width+6,[lblHead.text sizeForFont:13].height+3); y += bd + h; if (imgCnt > 0) { w = wid - 2* bd; h = w * 1.0; } for (int i =0; i < imgCntMax; i++) { UIView* v = imgs[i]; v.frame =CGRectMake(x, y, w, h); if (i < imgCnt) { y += h + bd; v.hidden = NO; }else{ v.hidden = YES; } } x = bd; h = 20; w = [lblTag.text sizeForFont:13].width+15; lblTag.frame =CGRectMake(x, y, w, h); lblTag.layer.cornerRadius = h/2.0; if (_showTag) { x += w+bd; lblTag.hidden = NO; }else{ lblTag.hidden = YES; } w = kSize.width - 200; lblAddr.frame =CGRectMake(x, y, w, h); for (UIView* v in btns) { v.hidden = YES; } } -(void)layoutStyle2 { CGFloat wid = self.frame.size.width; CGFloat x,y,w,h,bd; bd = 10; w = h = 50; x = y = bd; self.imageView.frame = CGRectMake(x, y, w, h); self.imageView.layer.cornerRadius = w/2.0; self.imageView.hidden = YES; self.textLabel.hidden = YES; self.detailTextLabel.hidden = YES; prtBtn.hidden = YES; x += bd+w; w = wid - x - bd; h /= 2.0; self.textLabel.frame =CGRectMake(x, y, w, h); y+= h; self.detailTextLabel.frame =CGRectMake(x, y, w, h); x = bd; w = wid - 2*bd; y = bd ; h = [lblContent.text heightForWid:w Font:NormalFont]+bd; lblContent.frame =CGRectMake(x, y, w, h); lblHead.frame =CGRectMake(x+1, y+3, [lblHead.text sizeForFont:13].width+6,[lblHead.text sizeForFont:13].height+3); if (imgCnt > 0) { y += bd + h; h = w = (wid - bd*4)/3.0; } int i = 0; for (UIView* v in imgs) { v.frame =CGRectMake(x, y, w, h); x += w+bd; if (i < imgCnt) { v.hidden = NO; }else{ v.hidden = YES; } i ++; } y += bd + h; x = bd; h = 20; // y = hei - h - bd; w = [lblTag.text sizeForFont:13].width+15; lblTag.frame =CGRectMake(x, y, w, h); lblTag.layer.cornerRadius = h/2.0; if (_showTag) { x += w+bd; lblTag.hidden = NO; }else{ lblTag.hidden = YES; } w = kSize.width - 200; lblAddr.frame =CGRectMake(x, y, w, h); h = 30; // y = hei - h - bd; w = h*2.5; x = wid - bd - w; UIButton* btn; btn = btns[1]; btn.frame =CGRectMake(x, y, w, h); x -= (bd + w ); btn = btns[0]; btn.frame =CGRectMake(x, y, w, h); } -(void)layoutStyle0 { CGFloat wid = self.frame.size.width; CGFloat x,y,w,h,bd; bd = 10; w = h = 50; x = y = bd; prtBtn.frame = self.imageView.frame = CGRectMake(x, y, w, h); self.imageView.layer.cornerRadius = w/2.0; x += bd+w; w = wid - x - bd; h /= 2.0; self.textLabel.frame =CGRectMake(x, y, w-100, h); y+= h; self.detailTextLabel.frame =CGRectMake(x, y, w, h); _acceptLabel.hidden = NO; _acceptLabel.frame = CGRectMake(kFrame.size.width - 90, y - h, 65, h); x = bd; w = wid - 2*bd; y += bd + h; h = [lblContent.text heightForWid:w Font:NormalFont]+bd; lblContent.frame =CGRectMake(x, y, w, h); lblHead.frame =CGRectMake(x+1, y+3, [lblHead.text sizeForFont:13].width+6,[lblHead.text sizeForFont:13].height+3); if (imgCnt > 0) { y += bd + h; h = w = (wid - bd*4)/3.0; } int i = 0; for (UIView* v in imgs) { v.frame =CGRectMake(x, y, w, h); x += w+bd; if (i < imgCnt) { v.hidden = NO; }else{ v.hidden = YES; } i ++; } y += bd + h; x = bd; h = 20; // y = hei - h - bd; w = [lblTag.text sizeForFont:13].width+15; lblTag.frame =CGRectMake(x, y, w, h); lblTag.layer.cornerRadius = h/2.0; // if (!lblTag.hidden) { // x += w+bd; // } if (_showTag) { x += w+bd; lblTag.hidden = NO; }else{ lblTag.hidden = YES; } w = kSize.width - 200; lblAddr.frame =CGRectMake(x, y, w, h); h = 30; // y = hei - h - bd; w = h*2.5; x = wid - bd - w; UIButton* btn; btn = btns[1]; btn.frame =CGRectMake(x, y, w, h); x -= (bd + w ); btn = btns[0]; btn.frame =CGRectMake(x, y, w, h); } -(void)btnClick:(UIButton*)sender { if (self.block) { self.block(); } if ( 0 == sender.tag) { if (!myDelegate.isLogin) { LoginViewController* vc = [[LoginViewController alloc] init]; vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; if (_delegate) { vc.modalPresentationStyle = UIModalPresentationFullScreen; [(UIViewController*)_delegate presentViewController:vc animated:YES completion:nil]; } return; } int pointNum = [[btns[0] titleForState:UIControlStateNormal] intValue]; if (sender.selected) { [(UIButton *)btns[0] setTitle:[NSString stringWithFormat:@"%d",pointNum -1] forState:UIControlStateNormal]; //didDePoint if ([_delegate respondsToSelector:@selector(didDePoint:)]) { [_delegate didDePoint:_model]; } }else{ [(UIButton *)btns[0] setTitle:[NSString stringWithFormat:@"%d",pointNum+1] forState:UIControlStateNormal]; if ([_delegate respondsToSelector:@selector(didPoint:)]) { [_delegate didPoint:_model]; } } sender.selected = !sender.selected; } if( 1 == sender.tag){ if ([_delegate respondsToSelector:@selector(gotoComment:)]) { [_delegate gotoComment:_model[@"ID"]]; } } if (2 == sender.tag) { if (_delegate) { DiaryVC* vc = [DiaryVC new]; NSString *cruser = _model[@"TI_CRUSER"]; if (!cruser || cruser.length == 0) { cruser = _model[@"CRUSER"]; } vc.userID = cruser; [(UIViewController*)_delegate navPushHideTabbarToVC:vc]; } } } @end CGFloat communityCellHeight(NSDictionary* dict , NSInteger type) { CGFloat wid = kSize.width; CGFloat bd = 10; if (0 == type) { NSString* str = dict[@"CONTENT"]; if (!str) { str = @""; } CGFloat contentH = [str heightForWid:(kSize.width - 20) Font:NormalFont] + 15; str = dict[@"PICTURES"]; CGFloat imgH ; if (!str || [str isEqualToString:@""]) { imgH = 0; }else{ imgH = (wid - bd*4)/3.0; } return 120 + contentH + imgH; } if (2 == type) { NSString* str = dict[@"CONTENT"]; if (!str) { str = @""; } CGFloat contentH = [str heightForWid:(kSize.width - 20) Font:NormalFont] + 15; str = dict[@"PICTURES"]; if (!str) { str = @""; } CGFloat imgH ; if ([str isEqualToString:@""]) { imgH = 0; }else{ imgH = (wid - bd*4)/3.0; } return 60 + contentH + imgH; } if (1 == type) { NSString* str = dict[@"CONTENT"]; if (!str) { str = @""; } CGFloat contentH = [str heightForWid:(kSize.width - 20) Font:NormalFont] + 15; str = dict[@"PICTURES"]; if (!str) { str = @""; } CGFloat imgH ; if ([str isEqualToString:@""]) { imgH = 0; }else{ NSArray *urls = [str componentsSeparatedByString:@","]; imgH = urls.count * (bd + (kSize.width - bd*2)*1.0); } return 120 + contentH + imgH; } return 400; } /**总共9个subView 其中1个是数组 前3个用cell自带的。后面的自己写. 下次复制的时候,首先就要把不同的地方改过来,然后再去修改个性化数据。 */