美文网首页
UI总结-tableView的界面传值

UI总结-tableView的界面传值

作者: Dear丶Musk | 来源:发表于2016-05-24 22:19 被阅读722次

       UI总结-tableView的界面传值

因为tableView在以后的开发占了很重要的地位,所以把tableView的界面传值单独拿出来做了这一篇,里面涉及了tableView界面之间的属性,协议传值和tableView的刷新等功能.

ViewController.m文件:

#import "ViewController.h"

#import "SecondViewController.h"

@interface ViewController ()

@property(nonatomic,retain)NSArray *arr1;

@property(nonatomic, retain)NSMutableArray *arr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

//定义数组,里面存数据

self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊",nil];

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

[self.view addSubview:tableView];

[tableView release];

tableView.dataSource = self;

tableView.delegate = self;

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click:)]autorelease];

tableView.tag = 1000;

[self creatData];

}

-(void)creatData{

//在本地找名为stu.plist的文件

NSString *path = [[NSBundle mainBundle]pathForResource:@"stu" ofType:@"plist"];

NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];

//将本地的数据显示在页面上

self.title = [dic valueForKey:@"name"];

}

//rightBarButtonItem的点击方法

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

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

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

[vc release];

}

//实现协议方法

-(void)sendValue:(NSString *)str{

//把传过来的人名加到属性数组里

[self.arr addObject:str];

UITableView *table = (UITableView *)[self.view viewWithTag:1000];

//刷新tableView

[table reloadData];

}

-(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:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];

}

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

return cell;

}

//tableView的点击方法

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

NSLog(@"%@",self.arr[indexPath.row]);

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

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

vc.str = self.arr[indexPath.row];

[vc release];

//签协议

vc.delegate = self;

}

secondController.h文件:

@protocol SecondViewControllerDelegate

- (void)sendValue:(NSString *)str;

@end

@interface SecondViewController : UIViewController@property(nonatomic, copy)NSString *str;

@property(nonatomic,assign)iddelegate;

@end

secondController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.title = self.str;

self.view.backgroundColor = [UIColor grayColor];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(100, 100, 150, 150);

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

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

button.backgroundColor = [UIColor redColor];

[self.view addSubview:button];

button.layer.borderWidth = 1;

button.layer.cornerRadius = 5;

}

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

[self.navigationController popToRootViewControllerAnimated:YES];

//协议传值

[self.delegate sendValue:@"水浒传"];

}

运行的效果:

相关文章

  • UI总结-tableView的界面传值

    UI总结-tableView的界面传值 因为tableView在以后的开发占了很重要的地位,所以把table...

  • UI总结-界面传值

    UI总结-界面传值(属性传值,协议传值,block传值,通知中指传值) 在编程过程中,界面传值是很重要的一部分,常...

  • UI界面传值

    传值需求 将用户信息 userInfo 作为传值对象进行传递。 场景一 主页传值到详情页 现在模拟传递用户名:us...

  • iOS中界面传值的几种方式

    1.属性传值 属性传值适用于顺序传值,从前面的界面传值给后面的界面。 2.代理传值 用代理的方式实现界面间传值稍微...

  • IOS开发 多界面传值

    本节学习内容: 1.多界面传值的基本概念 2.多界面传值的方法 3.多界面传值的应用 【多界面传值 属性】 cha...

  • ios界面传值2016.5

    五种方法 1.属性传值,适合界面A到界面B的传值2.单例, 多个界面传值3.通知 , 界面A跳...

  • swift中的协议代理传值

    协议代理传值一般使用在下级界面往上级界面传值的情况,这里将上级界面设定为A界面,下级界面设定为B界面。传值的具体操...

  • iOS开发-属性、block、代理、通知传值

    传值在开发中我们会经常用到,传值又分为正向传值和反向传值。从界面一跳转到界面二且将值从界面一传递给界面二使用,称之...

  • iOS 属性、代理、通知、Block传值

    实际开发中,几乎到处都会有用到传值,而传值分为正向传值以及逆(反)向传值,比如从界面一调到界面二,并将值从界面一传...

  • iOS的5种传值

    (-)属性传值 属性传值(场景)一般用于正向传值,即第一个界面传值给第二个界面 属性传值是这几大传值中最简单的传值...

网友评论

      本文标题:UI总结-tableView的界面传值

      本文链接:https://www.haomeiwen.com/subject/rbnzrttx.html