难点:由于有视频,语音,地理位置,文字,图片等不同的数据类型,所以相对来说布局叫复杂
主要的思路是:在获取数据的同时,计算好每个cell的布局,高度
1.数据类
@interface ShareInfoDemo : NSObject
@property(nonatomic,strong)NSString *idNum;
@property(nonatomic,strong)NSString *type;
@property(nonatomic,strong)NSString *content;
@property(nonatomic,strong)NSArray *referUris;
@property(nonatomic,strong)NSString *duration;
@property(nonatomic,strong)NSString *sendTime;
@property(nonatomic,strong)SelfDataModel *user;
@property(nonatomic,strong)NSMutableDictionary *replyPage;
@property(nonatomic,strong)NSMutableDictionary *zans;
@property(nonatomic,strong)NSString *location;
@property(nonatomic,assign)bool hasShowAll;
@end
2.计算每个cell
@interface ShareInfoFrame : NSObject
@property(nonatomic,strong)ShareInfoDemo *shareInfo;
@property(nonatomic,assign)CGRect iconImageFrame;
@property(nonatomic,assign)CGRect nameFrame;
@property(nonatomic,assign)CGRect contentFrame;
@property(nonatomic,strong)NSMutableArray * imageViewFArr;
@property(nonatomic,assign)CGRect mapFrame;
@property(nonatomic,assign)CGRect playviewFrame;
@property(nonatomic,assign)CGRect timeFrame;
@property(nonatomic,assign)CGRect blogToolBarF;
@property(nonatomic,strong)NSMutableArray *responseViewHeightArr;
@property(nonatomic,assign)CGFloat cellHeight;
@property(nonatomic,assign)CGRect zanFrame;
@property(nonatomic,assign)CGRect playSoundFrame;
@property(nonatomic,assign)CGRect delectFrame;
@property(nonatomic,assign)CGRect showAllFrame;
@end
.m文件
-(NSDictionary *)parseJSONStringToNSDictionary:(NSString *)JSONString {
NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil]; return responseJSON;
}
-(void)setShareInfo:(ShareInfoDemo *)shareInfo{
_shareInfo=shareInfo;
self.imageViewFArr=[[NSMutableArray alloc]initWithCapacity:0]; self.responseViewHeightArr=[[NSMutableArray alloc]initWithCapacity:0]; /
/头像的Frame CGFloat iconX = 20; CGFloat iconY = 20; CGFloat iconW = 40; CGFloat iconH = 40;
self.iconImageFrame=CGRectMake(iconX, iconY, iconW, iconH);
//名字的Frame CGFloat nameX = 70; CGFloat nameY = 20; CGFloat nameW = YBScreenBoundsWidth-90; CGFloat nameH =15;
self.nameFrame = CGRectMake(nameX, nameY, nameW, nameH);
//文字内容的Frame CGFloat contentX=70; CGFloat contentY=40; CGFloat contentW=YBScreenBoundsWidth-90; CGSize size1 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT);
NSDictionary *dict = @{NSFontAttributeName:ContentFont};
CGSize size2 =[self.shareInfo.content boundingRectWithSize:size1 options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; CGFloat contentH = size2.height;
self.contentFrame=CGRectMake(contentX, contentY, contentW, contentH);
if (contentH>90) { if (!self.shareInfo.hasShowAll) { self.contentFrame=CGRectMake(contentX, contentY, contentW, 90);
} } //富文本内容的Frame// bool isOver=NO;// if (size2.height+40<60) {// isOver=NO;//
}else{// isOver=YES;//
} if ([self.shareInfo.type isEqualToString:@"TEXT"]) {
if([self.shareInfo.content containsString:@"{"]){
NSDictionary *dic=[self parseJSONStringToNSDictionary:self.shareInfo.content]; //文字内容的Frame CGFloat contentX=70; CGFloat contentY=40; CGFloat contentW=YBScreenBoundsWidth-90; CGSize size1 = CGSizeMake(YBScreenBoundsWidth-90, MAXFLOAT); NSDictionary *dict = @{NSFontAttributeName:ContentFont}; CGSize size2 =[[NSString stringWithFormat:@"%@%@",dic[@"user"][@"name"],dic[@"message"]] boundingRectWithSize:size1 options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; CGFloat contentH = size2.height; self.contentFrame=CGRectMake(contentX, contentY, contentW, contentH);// if (contentH>90) {// if (!self.shareInfo.hasShowAll) {// self.contentFrame=CGRectMake(contentX, contentY, contentW, 90);// }// } } self.timeFrame=CGRectMake(70, CGRectGetMaxY(self.contentFrame)+10, 60,20); self.blogToolBarF=CGRectMake(YBScreenBoundsWidth-20-22, CGRectGetMaxY(self.contentFrame)+10, 22, 13); NSMutableString *mstr=[[NSMutableString alloc]initWithCapacity:0]; for (int i=0; i90&&![self.shareInfo.content containsString:@"{"]) {
self.showAllFrame=CGRectMake(210, self.timeFrame.origin.y-2, 60, 25);
}
NSArray *listArr=self.shareInfo.replyPage[@"list"];
if (![listArr isKindOfClass:[NSNull class]]) {
NSValue *lasevalue=[self.responseViewHeightArr lastObject];
CGRect rect=[lasevalue CGRectValue];
self.cellHeight=CGRectGetMaxY(rect)+20;
}else{
if (self.shareInfo.zans.allKeys.count==0) {
self.cellHeight=CGRectGetMaxY(self.timeFrame)+20;
}else{
self.cellHeight=CGRectGetMaxY(self.zanFrame)+20;
}
}
if (listArr.count==0) {
if (self.shareInfo.zans.allKeys.count==0) {
self.cellHeight=CGRectGetMaxY(self.timeFrame)+20;
}else{
self.cellHeight=CGRectGetMaxY(self.zanFrame)+20;
}
}
}
总结:看起来难的东西,只要思路对了就好了