国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

iOS中Cell高度如何能夠自動適應需要顯示的內容

2019-11-14 20:06:39
字體:
來源:轉載
供稿:網友

本文的代碼例子 : "Cell行高自適應.zhttp://vdisk.weibo.com/s/Gb9Mt

 

下面我們來看看代碼。我需要一個第三方庫EGO異步下載、addtion文件夾和StringUtil文件以及Comment、Status、User這三個數據模型,這篇文章的主要目的是講解如何計算Cell的高度,jSON數據分類見上面那篇文章,上面說的在代碼例子中都有的。將它們考入你的工程。

 

實現思路:

 

/* 

  File.strings

  Cell行高自適應

 

  Created by  on 13-4-2.

  Copyright (c) 2013甲. All rights reserved.

 

 實現類似微博這樣的項目需要怎么做?

 1、首先,要獲取數據。(這部分在網絡請求中介紹)

 2、其次,將數據分類保存在對應的模型中。(這個技術在大字典中介紹)

 3、最后,將獲取的數據顯示在屏幕上。

 

 一、為什么要學習Cell的行高自適應?

 因為,我們要將獲取的內容合理的顯示在屏幕上。

 

 二、實現步驟

 1、要先將數據發(fā)送到顯示界面的類ShowDataViewController

 2、計算獲取每個數據單元需要在屏幕上占多大的空間,這個功能的實現在類Algorithm中,詳細解釋在類中。

 3、創(chuàng)建一個UITableViewCell類型的類:StatusCell這個類的作用就是為了設計如何在Cell中顯示數據進行布局。

 

 三、技術難點:

 1、控件.autoresizingMask屬性:每個控件幾乎都有autoresizingMask的屬性,該屬性的作用是將改控件固定在相對的某個位置,例如:要將位置固定在當前cell的左下方,需要按下面的格式寫UIViewAutoresizingFlexibleTopMargin代表是上方、UIViewAutoresizingFlexibleRightMargin是右方的意思。因此寫代碼設置控件的相對位置需要寫出相反的方向。 

 

*/

 

 

我們先實現一個計算Cell高度的類:

Algorithm.h

 

[plain] view plaincopyPRint?
  1. <span >#import <Foundation/Foundation.h>  
  2. #import "Status.h"  
  3. @interface Algorithm : NSObject  
  4.   
  5. -(CGFloat)cellHeight:(NSString*)contentText with:(CGFloat)with;  
  6. //計算Cell的高度   
  7. -(CGFloat)calculation:(NSIndexPath*)indexPath StatusArr:(NSMutableArray*)statusArr;  
  8.   
  9.   
  10. @end  
  11. </span>  

 

 

Algorithm.m

 

  1. <span >#import "Algorithm.h"  
  2. #define kTextViewPadding            16.0  
  3. #define kLineBreakMode1              UILineBreakModeWordWrap  
  4. @implementation Algorithm  
  5. -(CGFloat)calculation:(NSIndexPath *)indexPath StatusArr:(NSMutableArray *)statusArr  
  6. {  
  7.     NSInteger row = indexPath.row;  
  8.     if (row >= [statusArr count]) {  
  9.         return 1;  
  10.     }  
  11.     Status* status = [statusArr objectAtIndex:row];  
  12.   
  13.     NSString* url = status.retweetedStatus.thumbnailPic;  
  14.       
  15.     NSString* url2 = status.thumbnailPic;  
  16.    
  17.     CGFloat height  = 0.0f;  
  18.       
  19.     if (status.retweetedStatus && ![status.retweetedStatus isEqual:[NSNull null]]) {  
  20.         //調用下面-(CGFloat)cellHeight:(NSString *)contentText with:(CGFloat)with的方法計算一段文字所占的高度  
  21.         height = [self cellHeight:status.text with:320.0f] + [self cellHeight:[NSString stringWithFormat:@"%@:%@",status.retweetedStatus.user.screenName,status.retweetedStatus.text] with:300.0f] - 22.0f;  
  22.         NSLog(@"status.retweetedStatus,height = %f",height);  
  23.     }  
  24.     else  
  25.     {  
  26.         height = [self cellHeight:status.text with:320.0f];  
  27.         //后加調式用  
  28.         height+=40;  
  29.         NSLog(@"height = %f",height);  
  30.     }  
  31.     if ((url && [url length] != 0) || (url2 && [url2 length] != 0)) {  
  32.         //如果有圖片將高度增加80個像素  
  33.         height = height + 80;  
  34.         NSLog(@"height = %f",height);  
  35.     }  
  36.     return height;  
  37.       
  38. }</span>  
  1. <span >//計算一段文字的高度  
  2. -(CGFloat)cellHeight:(NSString *)contentText with:(CGFloat)with  
  3. {  
  4.       
  5.     UIFont* font = [UIFont systemFontOfSize:14];  
  6.    //sizeWithFont:字體大小constrainedToSize:顯示的范圍lineBreakMode:  
  7.     CGSize size = [contentText sizeWithFont:font constrainedToSize:CGSizeMake(with - kTextViewPadding , 3000000.f) lineBreakMode:kLineBreakMode1];  
  8.     CGFloat height = size.height + 44;  
  9.     return height;  
  10. }  
  11. </span>  

 

 

接下來我們創(chuàng)建一個繼承自UITableViewCell

StatusCell.h

 

  1. <span >#import <UIKit/UIKit.h>  
  2. #import "Status.h"  
  3. #import "EGOImageView.h"  
  4. @interface StatusCell : UITableViewCell  
  5.   
  6. @property (nonatomic, retain) UITableViewCell* tableViewCell;  
  7. @property (nonatomic, retain) UITableView*     tableVw;  
  8. @property (nonatomic, retain) NSMutableArray* StatusArr;  
  9.   
  10. @property (retain, nonatomic)  UILabel *countLB;             //數量  
  11. @property (retain, nonatomic)  EGOImageView *avatarImage;  
  12. @property (retain, nonatomic)  UITextView *contentTF;        //微博正文  
  13. @property (retain, nonatomic) UILabel *userNameLB;  
  14. @property (retain, nonatomic)  UIImageView *bgImage;         //微博背景  
  15. @property (retain, nonatomic)  EGOImageView *contentImage;  //正文的圖片  
  16. @property (retain, nonatomic)  UIView *retwitterMainV;       //轉發(fā)的View  
  17. @property (retain, nonatomic)  EGOImageView *retwitterBgImage;     //轉發(fā)的背景  
  18. @property (retain, nonatomic)  UITextView *retwitterContentTF;//轉發(fā)的正文  
  19. @property (retain, nonatomic)  EGOImageView *retwitterContentImage;//轉發(fā)正文的圖片  
  20. //@property (assign, nonatomic) id<StatusViewCellDelegate> delegate;  
  21. @property (retain, nonatomic) NSIndexPath *cellIndexPath;  
  22. @property (retain, nonatomic)  UILabel *fromLB;  
  23. @property (retain, nonatomic)  UILabel *timeLB;  
  24. @property (retain, nonatomic)  UILabel* sourceLB;  //微博來源  
  25.   
  26.   
  27. -(CGFloat)setTFHeightWithImage:(BOOL)hasImage haveRetwitterImage:(BOOL)haveRetwitterImage;  
  28. -(void)setupCell:(Status*)status;  
  29.   
  30. -(void)controlPosition;  
  31.   
  32. @end  
  33. </span>  

StatusCell.m

 

本文中CELL中的控件全部用代碼實現,所以下面的代碼比較多。

 

  1. <span >#define IMAGE_VIEW_HEIGHT 80.0f  
  2. #import "StatusCell.h"  
  3.   
  4. @implementation StatusCell  
  5.   
  6. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  7. {  
  8.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  9.     if (self) {  
  10.    
  11.         // Initialization code  
  12.         //點擊Cell時背景不變藍  
  13.         self.selectionStyle = UITableViewCellSelectionStyleNone;  
  14.           
  15.         self.bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table_header_bg.png"]];  
  16.         self.bgImage.frame = CGRectMake(0, 29 , 320, 73);  
  17.        // self.bgImage.contentMode = UIViewContentModeScaleToFill;  
  18.         [self.contentView addSubview:self.bgImage];  
  19.           
  20.           
  21.         self.avatarImage = [[EGOImageView alloc] initWithFrame: CGRectMake(5.0 , 5.0, 22, 22)];  
  22.           
  23.         self.avatarImage.image = [UIImage imageNamed:@"loadingImage_50x118.png"];  
  24.         self.avatarImage.contentMode = UIViewContentModeScaleToFill;  
  25.         [self.contentView addSubview:self.avatarImage];  
  26.                               
  27.           
  28.         self.userNameLB = [[UILabel alloc] initWithFrame:CGRectMake(35, 0, 165, 28)];  
  29.         self.userNameLB.font = [UIFont systemFontOfSize:17.0];  
  30.         self.userNameLB.backgroundColor = [UIColor clearColor];  
  31.         [self.contentView addSubview:self.userNameLB];  
  32.           
  33.         self.contentTF = [[UITextView alloc] init];  
  34.         self.contentTF.editable = NO;  
  35.         self.contentTF.frame = CGRectMake(0, 29 , 320, 73);  
  36.         self.contentTF.font = [UIFont systemFontOfSize:14.0];  
  37.         //UITextView如果有網址可以直接打開  
  38.         self.contentTF.dataDetectorTypes = UIDataDetectorTypeLink;  
  39.         //開啟多點觸摸  
  40.         self.contentTF.multipleTouchEnabled = YES;  
  41.         self.contentTF.backgroundColor = [UIColor clearColor];  
  42.         [self.contentView addSubview:self.contentTF];  
  43.           
  44.           
  45.         self.contentImage = [[EGOImageView alloc] initWithImage:[UIImage imageNamed:@"loadingImage_50x118.png"]];  
  46.         self.contentImage.frame = CGRectMake(90, 84, 140, 80);  
  47.         self.contentImage.backgroundColor = [UIColor clearColor];  
  48.         [self.contentView addSubview:self.contentImage];  
  49.           
  50.           
  51.           
  52.           
  53.         self.retwitterMainV = [[UIView alloc] initWithFrame:CGRectMake(0, 166, 320, 160)];  
  54.         self.retwitterMainV.contentMode = UIViewContentModeScaleaspectFill;  
  55.         self.retwitterMainV.backgroundColor = [UIColor clearColor];  
  56.         [self.contentView addSubview:self.retwitterMainV];  
  57.           
  58.         //轉發(fā)背景圖  
  59.         self.retwitterBgImage = [[EGOImageView alloc] initWithFrame:CGRectMake(-10, -10, 320, 100)];  
  60.         self.retwitterBgImage.contentMode = UIViewContentModeScaleToFill;  
  61.         self.retwitterBgImage.backgroundColor = [UIColor clearColor];  
  62.         [self.retwitterMainV addSubview:self.retwitterBgImage];  
  63.           
  64.         //轉發(fā)的正文  
  65.         self.retwitterContentTF = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, 300, 150)];  
  66.         self.retwitterContentTF.editable = NO;  
  67.         self.retwitterContentTF.backgroundColor = [UIColor clearColor];  
  68.         [self.retwitterMainV addSubview:self.retwitterContentTF];  
  69.           
  70.         //轉發(fā)的圖片  
  71.         self.retwitterContentImage = [[EGOImageView alloc] initWithFrame:CGRectMake(90, 100, 140, 80)];  
  72.         self.retwitterContentImage.backgroundColor = [UIColor clearColor];  
  73.         self.retwitterContentImage.contentMode = UIViewContentModeScaleAspectFit;  
  74.         [self.retwitterMainV addSubview:self.retwitterContentImage];  
  75.           
  76.           
  77.         self.countLB = [[UILabel alloc] init];  
  78.         self.countLB.backgroundColor = [UIColor clearColor];  
  79.         [self.countLB setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:13.0]];  
  80.         self.countLB.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin;  
  81.         [self.contentView addSubview:self.countLB];  
  82.           
  83.         self.sourceLB = [[UILabel alloc] init];  
  84.         self.sourceLB.backgroundColor = [UIColor clearColor];  
  85.         [self.sourceLB setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:13.0]];  
  86.         //每個控件幾乎都有autoresizingMask的屬性,該屬性的作用是將改控件固定在相對的某個位置,例如:要將位置固定在當前cell的左下方,需要按下面的格式寫UIViewAutoresizingFlexibleTopMargin代表是上方、UIViewAutoresizingFlexibleRightMargin是右方的意思。因此寫代碼設置控件的相對位置需要寫出相反的方向。    
  87.         self.sourceLB.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin;  
  88.         [self.contentView addSubview:self.sourceLB];  
  89.           
  90.         self.timeLB = [[UILabel alloc] initWithFrame:CGRectMake(200, 0, 165, 28)];  
  91.         [self.timeLB setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:13.0]];  
  92.         self.timeLB.backgroundColor = [UIColor clearColor];  
  93.         [self.contentView addSubview:self.timeLB];  
  94.           
  95.         
  96.     }  
  97.     return self;  
  98. }</span>  
  1. <span >-(void)setupCell:(Status *)status  
  2. {  
  3.     //頭像圖片的網址  
  4.     NSURL* avatarImageUrl = [NSURL URLWithString:status.user.profileImageUrl];  
  5.     //正文圖片的網址  
  6.     NSURL* contentImageUrl = [NSURL URLWithString:status.thumbnailPic];  
  7.     //轉發(fā)圖片的方法  
  8.     NSURL* retweetedStatusUrl = [NSURL URLWithString:status.retweetedStatus.thumbnailPic];  
  9.       
  10.     self.contentTF.text = status.text;  
  11.     self.userNameLB.text = status.user.screenName;  
  12.     self.timeLB.text = [status timestamp];  
  13.     self.avatarImage.imageURL =avatarImageUrl;  
  14.       
  15.     self.countLB.text = [NSString stringWithFormat:@"評論:%d 轉發(fā):%d",status.commentsCount,status.retweetsCount];  
  16.     self.fromLB.text = [NSString stringWithFormat:@"來自:%@",status.source];  
  17.     self.timeLB.text = status.timestamp;  
  18.       
  19.     self.sourceLB.text = [NSString stringWithFormat:@"來源:%@",status.source];  
  20.     Status  *retwitterStatus    = status.retweetedStatus;  
  21.       
  22.     //有轉發(fā)  
  23.     if (retwitterStatus && ![retwitterStatus isEqual:[NSNull null]])  
  24.     {  
  25.         self.retwitterMainV.hidden = NO;  
  26.         self.retwitterContentTF.text = [NSString stringWithFormat:@"%@:%@",status.retweetedStatus.user.screenName,retwitterStatus.text];  
  27.         self.contentImage.hidden = YES;  
  28.           
  29.         if (![retweetedStatusUrl isEqual:[NSNull null]])  
  30.         {  
  31.             self.retwitterContentImage.imageURL = retweetedStatusUrl;  
  32.         }  
  33.           
  34.         NSString *url = status.retweetedStatus.thumbnailPic;  
  35.         self.retwitterContentImage.hidden = url != nil && [url length] != 0 ? NO : YES;  
  36.         [self setTFHeightWithImage:NO  
  37.                 haveRetwitterImage:url != nil && [url length] != 0 ? YES : NO];//計算cell的高度,以及背景圖的處理  
  38.     }  
  39.       
  40.     //無轉發(fā)  
  41.     else  
  42.     {  
  43.         self.retwitterMainV.hidden = YES;  
  44.         if (![contentImageUrl isEqual:[NSNull null]]) {  
  45.             self.contentImage.imageURL = contentImageUrl;  
  46.         }  
  47.           
  48.         NSString *url = status.thumbnailPic;  
  49.         self.contentImage.hidden = url != nil && [url length] != 0 ? NO : YES;  
  50.         [self setTFHeightWithImage:url != nil && [url length] != 0 ? YES : NO  
  51.                 haveRetwitterImage:NO];//計算cell的高度,以及背景圖的處理  
  52.     }  
  53. }</span>  
  1. <span >//計算cell的高度,以及背景圖的處理  
  2. -(CGFloat)setTFHeightWithImage:(BOOL)hasImage haveRetwitterImage:(BOOL)haveRetwitterImage  
  3. {  
  4.     [self.contentTF layoutIfNeeded];  
  5.       
  6.     //博文Text  
  7.     CGRect frame = self.contentTF.frame;  
  8.     frame.size = self.contentTF.contentSize;  
  9.     self.contentTF.frame = frame;  
  10.     //設置正文的背景  
  11.     self.bgImage.frame = frame;  
  12.       
  13.     //轉發(fā)博文Text  
  14.     frame = self.retwitterContentTF.frame;  
  15.     frame.size = self.retwitterContentTF.contentSize;  
  16.     self.retwitterContentTF.frame = frame;  
  17.     //調整轉發(fā)背景  
  18.     self.retwitterBgImage.image = [[UIImage imageNamed:@"timeline_rt_border_t.png"] stretchableImageWithLeftCapWidth:130 topCapHeight:7];  
  19.     frame.origin.x -=10;  
  20.     frame.origin.y -=5;  
  21.     frame.size.width = frame.size.width +15;  
  22.       
  23.     self.retwitterBgImage.frame = frame;  
  24.       
  25.     //轉發(fā)的主View  
  26.     frame = self.retwitterMainV.frame;  
  27.       
  28.     if (haveRetwitterImage) frame.size.height = self.retwitterContentTF.frame.size.height + IMAGE_VIEW_HEIGHT + 15;  
  29.     else frame.size.height = self.retwitterContentTF.frame.size.height + 15;  
  30.       
  31.     if(hasImage) frame.origin.y = self.contentTF.frame.size.height + self.contentTF.frame.origin.y + IMAGE_VIEW_HEIGHT;  
  32.     else frame.origin.y = self.contentTF.frame.size.height + self.contentTF.frame.origin.y;  
  33.       
  34.     self.retwitterMainV.frame = frame;  
  35.       
  36.       
  37.     //轉發(fā)的圖片  
  38.     frame = self.retwitterContentImage.frame;  
  39.     frame.origin.y = self.retwitterContentTF.frame.size.height;  
  40.     frame.size.height = IMAGE_VIEW_HEIGHT;  
  41.     self.retwitterContentImage.frame = frame;  
  42.     
  43.     //正文的圖片  
  44.     frame = self.contentImage.frame;  
  45.     frame.origin.y = self.contentTF.frame.size.height + self.contentTF.frame.origin.y - 5.0f;  
  46.     frame.size.height = IMAGE_VIEW_HEIGHT;  
  47.     self.contentImage.frame = frame;  
  48.      
  49.     //背景設置  
  50.     self.bgImage.image = [[UIImage imageNamed:@"table_header_bg.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];  
  51.       
  52.   //   = self.retwitterContentTF.frame;  
  53.     return self.contentTF.contentSize.height;  
  54. }</span>  
  1. <span >-(void)controlPosition  
  2. {  
  3.       self.countLB.frame = CGRectMake(198, self.frame.size.height - 20, 142, 21);  
  4.     self.sourceLB.frame = CGRectMake(20, self.frame.size.height - 20, 152, 21);  
  5.    
  6. }</span>  

 

 

再創(chuàng)建一個繼承自的UIViewController類ShowDataViewController這個類用來創(chuàng)建tableView以及顯示Cell中的內容。

ShowDataViewController.h

 

 

  1. <span >#import <UIKit/UIKit.h>  
  2. #import "Algorithm.h"  
  3. @interface ShowDataViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
  4. //創(chuàng)建一個UITableView  
  5. @property (strong, nonatomic) UITableView* tb;  
  6. //Algorithm類的作用是根據內容計算Cell的高度  
  7. @property (strong, nonatomic) Algorithm* algorithm;  
  8. //用一個NSMutableArray接收Status類的對象  
  9. @property (strong , nonatomic) NSMutableArray* statusArr;  
  10. @end  
  11. </span>  

ShowDataViewController.m

 

 

 

  1. <span >-(id)init  
  2. {  
  3.     if (self = [super init]) {  
  4.         //為什么要在init方法中注冊消息中心,這樣可以在AppDelegate方法中初始化這個類時注冊消息中心  
  5.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getArray:) name:@"getArray" object:nil];  
  6.     }  
  7.     return self;  
  8. }</span>  
  1. <span >-(void)getArray:(NSNotification*)aNotification  
  2. {  
  3.     //接收傳過來的數組,數組中存有Status類的對象  
  4.     self.statusArr = aNotification.object;  
  5. }</span>  
  1. <span >- (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view.  
  5.     self.tb = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 548) style:UITableViewStylePlain];  
  6.     self.tb.delegate = self;  
  7.     self.tb.dataSource = self;  
  8.     [self.view addSubview:self.tb];  
  9.     self.algorithm = [[Algorithm alloc] init];  
  10.       
  11.       
  12.      
  13. }</span>  
  1. <span >- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  2. {  
  3.     return [self.statusArr count];  
  4. }</span>  
  1. <span >-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     //調用Algorithm類的方法計算cell的高度 這個方法如何實現見Algorithm類中的注釋  
  4.     CGFloat height = [self.algorithm calculation:indexPath StatusArr:self.statusArr];  
  5.       
  6.     return height;  
  7. }</span>  
  1. <span >- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     int  row = indexPath.row;  
  4.    static NSString* strIdentifier = @"StatusCell";  
  5. //      
  6. //    //自定義Cell只需要將初始化的類變成自定義的Cell  
  7.     StatusCell* cell  = [tableView dequeueReusableCellWithIdentifier:strIdentifier];  
  8.     if (cell == nil)  
  9.     {  
  10.         cell =  [[StatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdentifier];  
  11.     }  
  12.     //下面這個方法是用來調整控件坐標的  
  13.     [cell controlPosition];  
  14.     //Status是儲存微博信息的類,將數組中的微博類的對象按照行號取出  
  15.     Status* status = [self.statusArr objectAtIndex:row];  
  16.     NSLog(@"row = %d",row);  
  17.     //下面這個方法是將微博信息放在Cell中  
  18.     [cell setupCell:status];  
  19.   
  20.       
  21.     return cell;  
  22.       
  23. }  
  24. </span>  

 

 

接下來我們在ViewController類中添加如下代碼

ViewController.h

 

 

  1. <span >@interface ViewController : UIViewController  
  2. -(IBAction)changeVC:(id)sender;  
  3. -(IBAction)sendData:(id)sender;  
  4. @end  
  5. </span>  

 

 

自己在XIB中拖2個button與下面兩個方法相關聯吧

  1. <span >-(IBAction)sendData:(id)sender  
  2. {  
  3.     //將JSON格式的數據文件的路徑找出  
  4.     NSString* path = [[NSBundle mainBundle] pathForResource:@"jsonTest" ofType:@"json"];  
  5.     //將數據放入NSData中  
  6.     NSData* data = [NSData dataWithContentsOfFile:path];  
  7.     //JSON解析  
  8.     NSDictionary* dic =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];  
  9.     //    NSLog(@"dic = %@",dic);  
  10.       
  11.       
  12.       
  13.       
  14.       
  15.     NSArray* arr = [dic objectForKey:@"statuses"];  
  16.     NSLog(@"%d",arr.count);  
  17.     NSLog(@"arr = %@",arr);  
  18.       
  19.       
  20.     //初始化一個數組  
  21.     NSMutableArray* tempArr = [NSMutableArray array];  
  22.     //將數組中的字典放入Status模型中,大家在這里會有一個疑問將數組中的字典都放入模型中,怎么區(qū)分不同數組中的數據?  
  23.     for (NSDictionary* item in arr) {  
  24.         //答案在statusWithJsonDictionary:的類方法中見該方法注釋  
  25.         Status* status = [Status statusWithJsonDictionary:item];  
  26.           
  27.           
  28.           
  29.           
  30.         //將Status類型的對象放入數組中  
  31.         [tempArr addObject:status];  
  32.     }  
  33.     //將tempArr數組通過消息中心發(fā)送到@"getArray" 這個名字的消息對應的方法中  
  34.     [[NSNotificationCenter defaultCenter] postNotificationName:@"getArray" object:tempArr];  
  35. }  
  36. </span>  

 

 

  1. <span >-(IBAction)changeVC:(id)sender  
  2. {  
  3.     //切換視圖控制器  
  4.     [[NSNotificationCenter defaultCenter] postNotificationName:@"changeVC" object:nil];  
  5.   
  6. }  
  7. </span>  

 

 

我們還要在AppDelegate類中添加如下代碼。

 

  1. <span >#import <UIKit/UIKit.h>  
  2. <span >#import "ShowDataViewController.h"  
  3. @class ViewController;</span><span >  
  4. </span>  
  5. @interface AppDelegate : UIResponder <UIapplicationDelegate>  
  6.   
  7. @property (strong, nonatomic) UIWindow *window;  
  8.   
  9. @property (strong, nonatomic) ViewController *viewController;  
  10. <span >@property (strong, nonatomic) ShowDataViewController* showVC;  
  11. </span>  
  12. @end</span>  

AppDelegate.m

 

 

  1. <span >#import "AppDelegate.h"  
  2. #import "ShowDataViewController.h"  
  3. <span >#import "ViewController.h"</span>  
  4.   
  5. @implementation AppDelegate  
  6.   
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  8. {</span>  
  1. <span ><span > </span>//為了注冊消息</span>  
  1. <span >   <span > [[ShowDataViewController alloc] init];</span></span>  
  1. <span ><span >//注冊消息為了切換視圖控制器  
  2.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeVC) name:@"changeVC" object:nil];</span>  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     // Override point for customization after application launch.  
  5.     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];  
  6.     self.showVC = [[ShowDataViewController alloc] init];  
  7.     self.window.rootViewController = self.viewController;  
  8.     [self.window makeKeyAndVisible];  
  9.     return YES;  
  10. }</span>  

 

 

 

  1. <span >-(void)changeVC  
  2. {  
  3.     self.window.rootViewController = self.showVC;  
  4. }</span>  

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 德令哈市| 万山特区| 台北市| 马尔康县| 美姑县| 阿坝| 达日县| 个旧市| 南江县| 阳谷县| 滦平县| 卢湾区| 嘉义县| 嘉善县| 蓝田县| 乾安县| 年辖:市辖区| 富锦市| 本溪| 海晏县| 长治县| 阜新市| 巴青县| 嘉鱼县| 武陟县| 龙口市| 浠水县| 闽侯县| 满洲里市| 浙江省| 延边| 柳河县| 景德镇市| 云林县| 长海县| 剑川县| 封开县| 仁寿县| 揭西县| 乌拉特前旗| 永修县|