IOS_UITableViewController 视图控制器的生命周期

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

// 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 = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    [_window release];

    

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

    UINavigationController *navi = [[UINavigationController alloc]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 = [super initWithStyle:style];

    if (self) {

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

    }

    return self;

}

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

- (void)loadView

{

    [super loadView];

    NSLog(@"加载试图");

}

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


- (void)viewDidLoad {

    [super viewDidLoad];

    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.tableView registerClass:[UITableViewCell class] 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 {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    NSLog(@"BOOM!!!");

}

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

{

    SecondViewController *secVC = [[SecondViewController alloc]init];

    [self.navigationController pushViewController:secVC animated:YES];

    [secVC release];

    //点击 变一下色

    //取消选择某一行

    [tableView deselectRowAtIndexPath:indexPath animated: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 = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];

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

//    if (!cell) {

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

//    }

    // Configure the cell...

    cell.textLabel.text = [NSString stringWithFormat:@"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 {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor redColor];

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值