此次的學習是在Navigation-based Application模板中,用RootViewController class設置操做方法,使用UITableView的屬性值。在導航控制器控件爲程序的窗口添加上導航條,可構建多個視圖鏈接導航按鈕。此次的練習中,我在Navigation controller控件加入兩個導航按鈕,屏幕左上角Add按鈕爲表格添加新的一行,右上角Edit按鈕爲表格刪除一行或者移動每行的順序。當user點擊edit按鈕後,便會進入到編輯的視圖,當user想要回到原先的視圖便點擊Done完成編輯。代碼以下。共同窗習,多提意見。。html
編碼:數組
ViewController.m文件ide
#import <UIKit/UIKit.h>學習
#import <Foundation/Foundation.h>編碼
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>atom
{url
IBOutlet UITableView *rootTableView;spa
IBOutlet UIButton *editButton;.net
NSMutableArray *dataArray;3d
UITextField *rowField;
}
@property (nonatomic,retain) NSMutableArray *dataArray;
@property (nonatomic, retain) UITextField *rowField;
@property (nonatomic, retain) IBOutlet UITableView *rootTableView;
@property (nonatomic, retain) IBOutlet UIButton *editButton;
ViewController.h文件
#import "ViewController.h"
@interface ViewController ()
@implementation ViewController
@synthesize rootTableView;
@synthesize dataArray;
@synthesize editButton;
@synthesize rowField;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//定義內容文字
dataArray = [[NSMutableArray alloc] initWithObjects:@"Row1", @"Row2",@"Row3",@"Row4",@"Row5",nil];
//創建add按鈕
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithTitle:@"Add"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(AddButtonAction:)];
self.navigationItem.leftBarButtonItem = addButton;
}
//構建操做方法,點擊按鈕執行
-(IBAction) EditButtonAction:(id)sender
{
// [rootTableView setEditing: YES
// animated: YES];
if ([sender tag] == 1) {
[editButton setTitle:@"Done" forState:UIControlStateNormal];
[editButton setTag:2];
[rootTableView setEditing:YES animated:YES];
}else if ([sender tag] == 2){
[editButton setTitle:@"Edit" forState:UIControlStateNormal];
[editButton setTag:1];
[rootTableView setEditing:NO animated:YES];
}
}
//添加addbutton
-(IBAction)AddButtonAction:(id)sender
{
UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"添加一行"
message:@""
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"肯定", nil];
rowField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 38.0, 245.0, 20.0)];
[rowField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:rowField];
[dialog show];
[dialog release];
[rowField release];
}
-(void) alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ((buttonIndex != [alertView cancelButtonIndex]) && (rowField.text != nil)) {
[dataArray insertObject:[rowField text] atIndex:dataArray.count];
[self.rootTableView reloadData];
}
}
//表格中分組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
//表格中行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//表格中行數等於數組內容的個數
if (section ==0) {
return dataArray.count;
}
if (section ==1)
{
return 0;
}
else {
return 0;
}
}
//分組標題內容設置
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//建立字符變量title
NSString *title = nil;
switch (section) {
case 0:
title = @"表格一";
break;
case 1:
title = @"表格二";
break;
default:
break;
}
return title;
}
//顯示錶格每一行內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//建立一個字符變量,用於取得文本數據類型
static NSString *CellIndentifier = @"cell";
//創建表格行數單元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
//當cell爲空時
if (cell == nil)
{
//爲cell從新獲取表格內容標識符
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
}
//分組section爲0
if (indexPath.section ==0)
{
cell.textLabel.text =
[dataArray 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)
{
//刪除
[dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
//上下行移動
-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[dataArray insertObject:[dataArray objectAtIndex:sourceIndexPath.row]
atIndex:destinationIndexPath.row];
[dataArray removeObjectAtIndex:(NSUInteger) sourceIndexPath.row+1];
}
//讓表格內容位置調整的方法
-(BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(void) dealloc
{
[rootTableView release];
rootTableView = nil;
[rowField release];
[dataArray release];
dataArray = nil;
[editButton release];
editButton = nil;
[super dealloc];
}
保存文件,打開ViewController.xib文件, 在view中添加一個Table view控件,
將delegate和dataSource鏈接到File's Owner圖標。
接着添加兩個Button控件,將add和edit鏈接到File's owner圖標上,保存文件運行:
運行程序:
當點擊Edit編輯,進入編輯視圖。