表視圖學習筆記





//  MainViewController.m

//  UI_TableView界面傳值

//

//  Created by dllo on 15/8/7.

//  Copyright (c) 2015 cml. All rights reserved.

//


#import "MainViewController.h"

#import "SecondViewController.h"

// 4.簽訂協議

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewController>

@property(nonatomic,retain)NSMutableArray *arr;

@property(nonatomic,retain)UITableView *tableView;

@property(nonatomic ,retain)UIImageView *imageView;

@end


@implementation MainViewController


-(void)dealloc{

    [_arr release];

    [_tableView release];

    [super dealloc];

}


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

    [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.arr=[NSMutableArray arrayWithObjects:@"宋江", @"盧俊義", @"吳用", @"公孫勝", @"關勝", @"林沖", @"秦明" ,@"呼延灼" , @"花榮",@"柴進", @"李應", @"朱仝",@"魯智深",@"武松",nil];

    }

    return self;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent =NO;

 

    self.tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];

    self.tableView.backgroundColor =[UIColor redColor];

    [self.view addSubview:self.tableView];

    [self.tableView release];

    self.tableView.rowHeight =100;

    self.tableView.delegate =self;

    self.tableView.dataSource=self;


    self.imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]]];

    self.imageView.frame =CGRectMake(0, -260, 375, 260);

//    [self.view addSubview:self.imageView];

    [self.imageView release];

    // tableView添加頭視圖

    // 寬是tableView的寬度

//    self.tableView.tableHeaderView =self.imageView;

    

    [self.tableView addSubview:self.imageView];

    

    self.tableView.contentInset =UIEdgeInsetsMake(260, 0, 0, 0);

    

}

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

    return self.arr.count;

}


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


    static NSString *reuse =@"reuse";

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];

    if (!cell) {

        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];

        

    }

    cell.textLabel.text =self.arr[indexPath.row];

    cell.detailTextLabel.text =[NSString stringWithFormat:@"%ld", indexPath.section];

    cell.imageView.image =[UIImage imageNamed:@"1.jpg"];

    return cell;

}


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

        

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

    

    secVC.name =self.arr[indexPath.row];

    

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

        [secVC release];

    // 5.設置代理人

    secVC.delegate =self;

        

 }

// 6.實現方法

-(void)change:(NSString *)value{

    // 屬性的數組,相當於數據源,把傳過來的織田見到數組中

    if(![value isEqualToString:@""]){

    [self.arr addObject:value];

    //tableView進行刷新操作

    [self.tableView reloadData];

    }

}



#pragma mark tableView delegate已經簽訂好scrollView的協議,只要設置代理人,就可以使用scrollView的協議方法

// 只要滑動就會觸發

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

    NSLog(@"滑動");

    

    // 獲取偏移量

    CGFloat y =scrollView.contentOffset.y;

    NSLog(@"%g",y);

    

    if (y<-260) {

        self.imageView.frame =CGRectMake(0, y, self.view.frame.size.width, -y);

    }

  

}

//  SecondViewController.h

//  UI_TableView界面傳值

//

//  Created by dllo on 15/8/7.

//  Copyright (c) 2015 cml. All rights reserved.

//


#import <UIKit/UIKit.h>


// 聲明一份協議


@protocol SecondViewController <NSObject>


-(void)change:(NSString *)value;


@end


@interface SecondViewController : UIViewController

@property(nonatomic,copy)NSString *name;


// 設置代理人屬性

@property(nonatomic ,assign)id<SecondViewController>delegate;


@end

//  SecondViewController.m

//  UI_TableView界面傳值

//

//  Created by dllo on 15/8/7.

//  Copyright (c) 2015 cml. All rights reserved.

//


#import "SecondViewController.h"


@interface SecondViewController ()<UITextFieldDelegate>

@property(nonatomic,retain)UILabel *label;

@property(nonatomic,retain)UITextField *textField;

@property(nonatomic ,retain)UIButton *button;

@end


@implementation SecondViewController

-(void)dealloc{

    [_label release];

    [_textField release];

    [_button release];

    [_name release];

    [super dealloc];

}



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor =[UIColor yellowColor];

    // 建一個 label

    self.label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

    self.label.backgroundColor =[UIColor redColor];

    self.label.layer.borderWidth =1;

    self.label.layer.cornerRadius =5;

    self.label.layer.masksToBounds=YES;

    // label進行賦值

    self.label.text =self.name;

    [self.view addSubview:self.label];

    [self.label release];

    // 建一個 textField

    self.textField =[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 30)];

    self.textField.layer.borderWidth =1;

    self.textField.layer.cornerRadius =5;

    

    self.textField.backgroundColor =[UIColor cyanColor];

    

    [self.view addSubview:self.textField];

    [self.textField release];

    

    // 建一個 button

    self.button =[UIButton buttonWithType:UIButtonTypeSystem];

    self.button.frame=CGRectMake(100, 300, 100, 30);

    [self.view addSubview:self.button];

    self.button.layer.borderWidth=1;

    self.button.layer.cornerRadius=5;

    [self.button setTitle:@"返回" forState:UIControlStateNormal];

    self.button.backgroundColor =[UIColor redColor];

    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    self.textField.delegate =self;

    

    

}


-(void)click:(UIButton *)button{

    [self.navigationController popToRootViewControllerAnimated:YES];

    [self.delegate change:self.textField.text];


}

#pragma mark 點擊return 回收鍵盤

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    

    [self.textField endEditing:YES];

    return YES;

}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章