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:@"水浒传"];
}