TableView界面传值

来源:互联网 发布:木疙瘩软件下载 编辑:程序博客网 时间:2024/05/02 10:37

TableView界面传值

import

import “SecondViewController.h”

@interface SecondViewController ()
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,retain)UIButton *button;

@end

@implementation SecondViewController

  • (void)dealloc
    {
    [_str release];
    [_button release];
    [_label release];
    [_textField release];
    [super dealloc];
    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor yellowColor];

    self.label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
    self.label.backgroundColor=[UIColor redColor];
    [self.view addSubview:self.label];
    [self.label release];

    self.textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 175, 50)];
    self.textField.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.textField];
    [self.textField release];
    self.textField.layer.borderWidth=1;
    self.textField.layer.cornerRadius=10;

    self.button=[UIButton buttonWithType:(UIButtonTypeSystem)];
    self.button.frame=CGRectMake(300, 200, 70, 50);
    [self.button setTitle:@”返回” forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }

-(void)click:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
// 3.设置协议方法
[self.delegete changeValue:self.textField.text];
}

import “MainViewController.h”

import “SecondViewController.h”

// 4.签协议
@interface MainViewController ()

pragma mark tableView的delegate已经签订好scrollView的协议,只要设置好代理人,就可以使用了=scrollView的协议方法

// 只要滑动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 获取偏移量
CGFloat y=scrollView.contentOffset.y;
NSLog(@”%g”,y);
if (y<0) {
self.imageView.frame=CGRectMake(0, y, 375, -y);
}
}
- (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:UITableViewCellStyleSubtitle reuseIdentifier:reuse];

    }
    cell.textLabel.text=self.arr[indexPath.row];
    cell.detailTextLabel.text=[NSString stringWithFormat:@”%ld”,indexPath.section];
    cell.imageView.image=[UIImage imageNamed:@”11.jpg”];

    return cell;
    }

  • (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{
    SecondViewController *secVC=[[SecondViewController alloc] init];
    secVC.str=self.arr[indexPath.row];
    // 5.设置代理人
    secVC.delegete=self;
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
    }

// 6.实现协议方法
-(void)changeValue:(NSString *)name{
// 属性的数组,相当于数据源,把传过来的值添加到数组中
[self.arr addObject:name];
// 对tableView进行刷新操作
[self.tableView reloadData];
}

0 0