? ? ? ?UI總結(jié)-tableView的界面?zhèn)髦?br>
因為tableView在以后的開發(fā)占了很重要的地位,所以把tableView的界面?zhèn)髦祮为毮贸鰜碜隽诉@一篇,里面涉及了tableView界面之間的屬性,協(xié)議傳值和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];
//定義數(shù)組,里面存數(shù)據(jù)
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"盧俊義", @"吳用", @"公孫勝", @"關(guān)勝", @"林沖", @"秦明" ,@"呼延灼" , @"花榮",@"柴進", @"李應(yīng)", @"朱仝",@"魯智深",@"武松", @"徐寧", @"張清", @"楊志", @"董平", @"索超", @"戴宗", @"劉唐", @"李逵", @"史進", @"穆弘", @"雷橫", @"李俊",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];
//將本地的數(shù)據(jù)顯示在頁面上
self.title = [dic valueForKey:@"name"];
}
//rightBarButtonItem的點擊方法
-(void)click:(UIBarButtonItem *)button{
SecondViewController *vc = [[SecondViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
//實現(xiàn)協(xié)議方法
-(void)sendValue:(NSString *)str{
//把傳過來的人名加到屬性數(shù)組里
[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];
//簽協(xié)議
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];
//協(xié)議傳值
[self.delegate sendValue:@"水滸傳"];
}