仿微信朋友圈以及新浪微博

難點:由于有視頻,語音,地理位置,文字,圖片等不同的數據類型,所以相對來說布局叫復雜

主要的思路是:在獲取數據的同時,計算好每個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;

}

}

}


總結:看起來難的東西,只要思路對了就好了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • VLC的集成和使用 VLC介紹 VLC Media Player (VideoLAN) 為 Windows、Lin...
    Pocket閱讀 19,852評論 75 66
  • iOS開發系列--網絡開發 概覽 大部分應用程序都或多或少會牽扯到網絡開發,例如說新浪微博、微信等,這些應用本身可...
    lichengjin閱讀 3,712評論 2 7
  • 不等高cell高度計算 1.首先看效果圖 2.重難點: 對于本例來講最重要的就是計算cell的高度,以及理解mvc...
    Alice三劍客閱讀 614評論 0 2
  • 表妹是我舅舅的女兒,91年出生。舅舅是**軍區的軍官,后轉業回家工作。表妹出生在軍營里,舅媽作為隨軍家屬一直帶著表...
    蝸牛郎君閱讀 160評論 0 0
  • 甲午年秋,余別故土,只身如贛,求學洪都。凡經兩年,雖無心智之長,亦有世事之識。如今山居紫廬,松風陣陣,水聲汩汩,...
    灼灼新桃閱讀 1,213評論 0 4