123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737 |
- #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<cnt; i++) {
- UIImageView* iv;
- iv = [[UIImageView alloc] init];
- /*
- UIViewContentModeScaleToFill根据屏幕拉宽
- UIViewContentModeScaleAspectFit原大小
- UIViewContentModeScaleAspectFill放大获取合适的宽 只是长被裁减了
- */
- // 内容模式
- [iv setContentMode:UIViewContentModeScaleAspectFit];
- iv.layer.masksToBounds = YES;
-
- if (st == 1) {
- // 事件监听 这是详情里边的 最多九张图
- iv.tag = i;
- iv.userInteractionEnabled = YES;
- [iv addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:selfCell action:@selector(tapImage:)]];
- iv.clipsToBounds = YES;
- iv.contentMode = UIViewContentModeScaleAspectFit;
- }
-
- [cell addSubview:iv];
- [cell.imgs addObject:iv];
- }
-
- [cell setBackgroundColor:[UIColor clearColor]];
- [cell.textLabel setTextColor:kTitleColor];
- [cell.detailTextLabel setTextColor:contentTextColor];
-
- cell.imageView.layer.masksToBounds = YES;
-
- }
- return cell;
- }
- -(void)tapImage:(UITapGestureRecognizer *)tap
- {
- //隐藏键盘
- if (self.block) {
- self.block();
- }
- [RQ_SHARE_FUNCTION showPhotoBrowserWithDataSource:urls currentIndex:tap.view.tag isCanSave:YES];
- }
- //这个方法总是执行两次 不晓得什么情况 不过现在不影响了
- -(void)setModel:(NSDictionary *)model
- {
- _model = model;
-
- urls = [model[@"PICTURES"] componentsSeparatedByString:@","];
-
- // NSLog(@"urls->%@",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自带的。后面的自己写.
- 下次复制的时候,首先就要把不同的地方改过来,然后再去修改个性化数据。
- */
|