難點:由于有視頻,語音搅幅,地理位置阅束,文字,圖片等不同的數(shù)據(jù)類型茄唐,所以相對來說布局叫復(fù)雜
主要的思路是:在獲取數(shù)據(jù)的同時息裸,計算好每個cell的布局,高度
1.數(shù)據(jù)類
@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);? ?
//文字內(nèi)容的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);? ? ??
}? ? }? ? //富文本內(nèi)容的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];? ? ? ? ? ? //文字內(nèi)容的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;
}
}
}
總結(jié):看起來難的東西沪编,只要思路對了就好了