您的位置:首页 > 产品设计 > UI/UE

iphone开发 有关 Navigation Bar 和 UITableView 的用法(代码加说明 呵呵)

2012-06-28 12:39 267 查看
#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@property (strong,nonatomic)UINavigationController *navigation;

@end


首先 在AppDelegate.h中 加入 UINavigationController 这个 属性 ,在程序一加载就 初始话 一个UINavigation Bar 导航栏

那么在AppDelegate.m 实现中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

self.navigation=[[UINavigationController alloc] initWithRootViewController:self.viewController];

self.window.rootViewController = self.viewController;

[self.window addSubview:self.navigation.view ];

[self.window makeKeyAndVisible];
return YES;
}


在实现的 didFinishLaunchingWithOptions方法 中 修改 代码

我们用的是 单视图的模板 原来 方法中是 初始化 window 和 viewController 这些 属性

我们要加的就是 初始话 我们的navigation 这里我们的 RootViewController 是 viewControler 来初始化

然后就是 把 view 和navigation 加载到 window中 顺序是 window ------view

navigation

接下来 在视图xib文件 中 拖入view中 添加连接

viewConroller.h 中 要实现 tableView 中的两个委托 UITableViewDelegate 和 UITableViewDataSource 应为我们继承的是UIViewController

如果我们继承了UITableViewController的话 就会自动帮我们生成 委托里的方法

#import <UIKit/UIKit.h>

@interface  ViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>

@property ( nonatomic,retain) IBOutlet UITableView *tableView;

@property ( nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITextField *text;

@end


viewController.m 中

这里需要初始话 我们的 tableView

委托里的方法 都是 用SELF.tableView 来调用方法的

#import "ViewController.h"
@implementation ViewController
@synthesize tableView;
@synthesize arr ;
@synthesize text;

#pragma mark - view lifeCycle

UIBarButtonItem *edit=nil;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (!tableView) {
tableView=[[UITableView alloc]initWithFrame:CGRectMake(320.0, 460.0, 320.0, 460.0)];
}

//[self.tableView setEditing:YES animated:YES];
//[self.tableView  setEditing:NO animated:YES];

//self.navigationItem.rightBarButtonItem=self.editButtonItem;

arr=[[NSMutableArray alloc] initWithObjects:@"Row0", @"Row1",@"Row2",@"Row3",@"Row4",nil];

UIBarButtonItem *add=[[UIBarButtonItem alloc ] initWithTitle:@"Add" style:UIBarButtonItemStyleBordered target:self action:@selector(AddAction)];

self.navigationItem.leftBarButtonItem=add;

self.title=@"xiao表格";

edit=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(editAction)];

self.navigationItem.rightBarButtonItem=edit;

}

-(void)editAction
{
if (edit.title==@"编辑") {
[edit setTitle:@"确定"];
[edit setStyle:UIBarButtonItemStyleBordered];

[self.tableView setEditing:YES animated:YES];

}else {
[edit setTitle:@"编辑"];
[edit   setStyle:UIBarButtonItemStylePlain  ];

[self.tableView setEditing:NO animated:YES];
}

}

//复用  uialertview的方法

NSUInteger a=0;

NSIndexPath * indext=nil;
-(void)MyAlertView:(NSUInteger)t
{
UIAlertView *alert=nil;
if (t==0) {
alert=[[UIAlertView alloc] initWithTitle:@"添加行" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
a=1;

}
if (t==1) {
alert=[[UIAlertView alloc] initWithTitle:@"选中行的内容" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

a=2;
}

text=[[UITextField alloc] initWithFrame:CGRectMake(30.0, 35.0, 220.0, 25.0)];

[text setBackgroundColor:[UIColor  whiteColor]];

[alert addSubview:text];

[alert show];

}

//  添加  点击背景  关闭键盘  方法
-(IBAction)backgroundTap:(id)sender
{
[text resignFirstResponder];

}

-(void)AddAction
{
NSLog(@"%@",@"tian  jia   an  niu  ");

NSUInteger  b=0;
[self MyAlertView:b];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

if (a==1) {
if (buttonIndex!=[alertView cancelButtonIndex ]&&text.text!=nil) {

[arr insertObject:text.text atIndex:arr.count];

//NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadData];
}

}
if (a==2) {

text.text=[arr objectAtIndex:(NSUInteger)indext.row];

NSLog(@"indext.row=%d",indext.row);

NSLog(@"text.text=%@",text.text);

if (buttonIndex!=[alertView cancelButtonIndex ]&&text.text!=nil&&indext!=nil) {

[arr insertObject:text.text atIndex:indext.row];

[arr removeObjectAtIndex:(NSUInteger)indext.row ];

NSLog(@"indext.row=%d", indext.row  );

[self.tableView reloadData  ];

}

}

}

#pragma mark - Table view data  source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%d",section);

return  arr.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title=@"MyTableView";

return title;
}

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

static NSString *cellInditified=@"Cell";

UITableViewCell *cell=[[UITableViewCell  alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInditified];

/*
if (indexPath.section==0) {
cell.textLabel.text=[NSString stringWithFormat:@"Catagory1  Row%d",indexPath.row];
}
if (indexPath.section==1) {
cell.textLabel.text=[NSString stringWithFormat:@"Catagory2  Row%d",indexPath.row];
}*/

cell.textLabel.text=[arr objectAtIndex:indexPath.row ];

return cell;

}

//启用编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{

return  YES;
}

//编辑      插入和删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

if (editingStyle==UITableViewCellEditingStyleDelete) {

NSLog(@"%@",@"删除");

[arr removeObject:[arr objectAtIndex:(NSUInteger)indexPath.row]];

[self.tableView  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

[self.tableView reloadData];
}else if (editingStyle==UITableViewCellEditingStyleInsert) {

NSLog(@"%@",@"编辑");

}

}

//允许用户调换行 位置
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{

return  YES;
}

//  调换行的位置时     让 arr中的源  元素 插入到目标元素 的位置     移除源元素 重新让tableview加载数据
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{

[arr insertObject:[arr objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath  .row];

//[arr removeObject:[arr objectAtIndex:destinationIndexPath.row]];
[arr removeObjectAtIndex:(NSUInteger)destinationIndexPath.row+1];

//[self.tableView reloadData];
}

#pragma mark -

#pragma mark   Table  view delegate

//当tableview中的  行被选中后    触发事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
indext=indexPath;

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失

[self MyAlertView:1];

}

- (void)viewDidUnload
{

[self setTableView:nil];
[super viewDidUnload];
[self setArr:nil];
[self setText:nil];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: