IOS_UITableViewController 视图控制器的生命周期

来源:互联网 发布:南望王师又一年 知乎 编辑:程序博客网 时间:2024/06/06 13:13

// 视图控制器的生命周期

// 1. 初始化方法(init)

// 2.如果视图需要显示在屏幕上(View的getter方法被调用) 就会调用loadView方法创建视图,准备显示

// 3 .创建视图之后,调用-viewDidLoad方法创建自定义的子视图

//4. 反复调用视图出现和消失4个方法

// 5. dealloc被销毁,回收内存

#import <UIKit/UIKit.h>


@interface AppDelegate :UIResponder <UIApplicationDelegate>


@property (strong,nonatomic) UIWindow *window;



@end


#import "AppDelegate.h"

#import "MainTableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    [_window release];

    

    MainTableViewController *mainVC = [[MainTableViewControlleralloc]initWithStyle:UITableViewStylePlain];

    UINavigationController *navi = [[UINavigationControlleralloc]initWithRootViewController:mainVC];

        self.window.rootViewController = navi;

        [mainVC release];

        [navi release];

 

    return YES;

}

#import <UIKit/UIKit.h>


@interface MainTableViewController : UITableViewController


@end


#import "MainTableViewController.h"


#import "SecondViewController.h"

@interface MainTableViewController ()

@property (nonatomic,retain)UIScrollView *scrollVeiw;

@property (nonatomic,retain)UIPageControl *pageControl;

@end


@implementation MainTableViewController



// 视图控制器的生命周期

// 1. 初始化方法(init)

// 2.如果视图需要显示在屏幕上(View的getter方法被调用) 就会调用loadView方法创建视图,准备显示

// 3 .创建视图之后,调用-viewDidLoad方法创建自定义的子视图

//4. 反复调用视图出现和消失4个方法

// 5. dealloc被销毁,回收内存

//视图控制器初始化方法

- (instancetype)initWithStyle:(UITableViewStyle)style

{

    self = [superinitWithStyle:style];

    if (self) {

        NSLog(@"初始化方法");

    }

    return self;

}

//没有navigation的的时候调用loadView

- (void)loadView

{

    [super loadView];

    NSLog(@"加载试图");

}

//- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil


- (void)viewDidLoad {

    [superviewDidLoad];

    NSLog(@"视图已经加载");

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    //可以提前给tableView说明一下cell类对应的重用标识,声明之后 下面不用写if判断了

    [self.tableViewregisterClass:[UITableViewCellclass] forCellReuseIdentifier:@"reuseIdentifier"];

    //自定义cell时这么写

//      [self.tableView registerClass:[mycell class] forCellReuseIdentifier:@"reuseIdentifier"];

    {

//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 330, 200)];

//    //前三个参数没有用 都是满的从头开始 重要的是控制高度

//    view.backgroundColor = [UIColor yellowColor];

//    self.tableView.tableHeaderView = view;

//    //在上面加scollview

////    self.tableView.tableFooterView = view;

//    //在上面加button

//    

//////    self.scrollVeiw = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 20, 335, 200)];

//     self.scrollVeiw = [[UIScrollView alloc]initWithFrame:self.tableView.bounds];

//    self.scrollVeiw.backgroundColor = [UIColor yellowColor];

//    [self.tableView.tableHeaderView addSubview:self.scrollVeiw];

//    [_scrollVeiw release];

//    //如果滑动的页数是4

//    NSInteger num = 4;

//    for (NSInteger i = 0 ; i < num; i++) {

//        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*self.scrollVeiw.bounds.size.width, 0, self.scrollVeiw.bounds.size.width, 200)];

//        NSString *name = [NSString stringWithFormat:@"%ld.jpg",i];

//        imageView.image = [UIImage imageNamed:name];

//        

//        [self.scrollVeiw addSubview:imageView];

//        [imageView release];

//    }

//    

//    //设置滚动范围

//    self.scrollVeiw.contentSize = CGSizeMake(self.scrollVeiw.bounds.size.width*4, 0);

//    self.scrollVeiw.pagingEnabled = YES;

//    self.scrollVeiw.delegate = self;

//    

//    //

//    //

////    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(20,200,335,40)];

////    self.pageControl.backgroundColor = [UIColor blackColor];

//    //设置数量

//    self.pageControl.numberOfPages = num;

//    //设置点的颜色

//    //非当前选择的点的颜色

//    self.pageControl.pageIndicatorTintColor = [UIColor redColor];

//    

//    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

//    [self.scrollVeiw addSubview:self.pageControl];

//    [_pageControl release];


    }

    

}

/*{

//- (void)pageAction:(UIPageControl *)pageC

//{

//    

//    NSLog(@"换页");

//    //点击pageControl控制scrollView

//    //    self.scrollVeiw.contentOffset = CGPointMake(335*pageC.currentPage, 0);

//    //添加动画效果

//    [self.scrollVeiw setContentOffset:CGPointMake(335*pageC.currentPage, 0) animated:YES];

//    

//    

//    

//    

//}

////宽度*页数=偏移量

////同scroll协议控制pagecontrol 知道偏移量 宽度 求页数

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView

//{

//    self.pageControl.currentPage = scrollView.contentOffset.x/335;

//    

//}

}*/


//创建视图的四种方法

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    NSLog(@"视图将要出现");

}

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    NSLog(@"视图已经出现");

}

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    NSLog(@"视图将要消失");

}

- (void)viewDidDisappear:(BOOL)animated

{

    [super viewDidDisappear:animated];

    NSLog(@"视图已经消失");

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    NSLog(@"BOOM!!!");

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    SecondViewController *secVC = [[SecondViewControlleralloc]init];

    [self.navigationControllerpushViewController:secVC animated:YES];

    [secVC release];

    //点击 变一下色

    //取消选择某一行

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

    

    

}

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Potentially incomplete method implementation.

    // Return the number of sections.

    return 2;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete method implementation.

    // Return the number of rows in the section.

    return 10;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"reuseIdentifier"forIndexPath:indexPath];

    //提前说明了 下面就不用写了

//    if (!cell) {

//        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reuseIdentifier"]autorelease];

//    }

    // Configure the cell...

    cell.textLabel.text = [NSStringstringWithFormat:@"section:%ld,Row:%ld",indexPath.section,indexPath.row];

    return cell;


}



#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController


@end

#import "SecondViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColorredColor];

}




0 1
原创粉丝点击