zoukankan      html  css  js  c++  java
  • iOS中Cell高度如何能够自动适应需要显示的内容

    本文的代码例子 : "Cell行高自适应.zip" http://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、要先将数据发送到显示界面的类ShowDataViewController

     2、计算获取每个数据单元需要在屏幕上占多大的空间,这个功能的实现在类Algorithm中,详细解释在类中。

     3、创建一个UITableViewCell类型的类:StatusCell这个类的作用就是为了设计如何在Cell中显示数据进行布局。

     

     三、技术难点:

     1、控件.autoresizingMask属性:每个控件几乎都有autoresizingMask的属性,该属性的作用是将改控件固定在相对的某个位置,例如:要将位置固定在当前cell的左下方,需要按下面的格式写UIViewAutoresizingFlexibleTopMargin代表是上方、UIViewAutoresizingFlexibleRightMargin是右方的意思。因此写代码设置控件的相对位置需要写出相反的方向。 

     

    */

     

     

    我们先实现一个计算Cell高度的类:

    Algorithm.h

     

    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>  

    接下来我们创建一个继承自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;       //转发的View  
    17. @property (retain, nonatomic)  EGOImageView *retwitterBgImage;     //转发的背景  
    18. @property (retain, nonatomic)  UITextView *retwitterContentTF;//转发的正文  
    19. @property (retain, nonatomic)  EGOImageView *retwitterContentImage;//转发正文的图片  
    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.         //转发背景图  
    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.         //转发的正文  
    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.         //转发的图片  
    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.     //转发图片的方法  
    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 转发:%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.     //有转发  
    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.     //无转发  
    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.     //转发博文Text  
    14.     frame = self.retwitterContentTF.frame;  
    15.     frame.size = self.retwitterContentTF.contentSize;  
    16.     self.retwitterContentTF.frame = frame;  
    17.     //调整转发背景  
    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.     //转发的主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.     //转发的图片  
    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>  

    再创建一个继承自的UIViewController类ShowDataViewController这个类用来创建tableView以及显示Cell中的内容。

    ShowDataViewController.h

     

     

    1. <span >#import <UIKit/UIKit.h>  
    2. #import "Algorithm.h"  
    3. @interface ShowDataViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
    4. //创建一个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模型中,大家在这里会有一个疑问将数组中的字典都放入模型中,怎么区分不同数组中的数据?  
    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数组通过消息中心发送到@"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>  
  • 相关阅读:
    Mac-安装Git以及Git的配置
    Mac 安装Maven,并设置环境变量
    Mac Tab自动补全键
    Eclipse 代码快捷键模板(一)
    网易博客迁移(2011-05-27)
    前端JS插件整理
    Ajax请求二进制流并在页面展示
    IDE中使用System.getProperty()获取一些属性
    Spring Boot:快速入门(二)
    c 语言 指针 与地址
  • 原文地址:https://www.cnblogs.com/lovewx/p/3929346.html
Copyright © 2011-2022 走看看