2011-12-02 65 views
0

我不知道如果我正在接近這個正確的方式,所以我需要一些幫助。我有一個5行的表格供輸入數據。用戶輸入所有數據後,導航欄上會有一個按鈕將數據添加到陣列。我想設置另一個視圖控制器來顯示用戶輸入的所有數據。我希望這是有道理的。這是迄今爲止我所擁有的。UITableview與文本字段到陣列

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

[self initWithStyle: UITableViewStyleGrouped]; 
self.navigationController.navigationBarHidden=NO; 
//self.navigationItem.hidesBackButton = YES; 

// Uncomment the following line to preserve selection between presentations. 
//self.clearsSelectionOnViewWillAppear = NO; 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"Done" style:UIBarButtonItemStylePlain target: self action: @selector(add)] autorelease]; 

course = [[NSMutableArray alloc] init]; 
date = [[NSMutableArray alloc] init]; 
scores = [[NSMutableArray alloc] init]; 
rating = [[NSMutableArray alloc] init]; 
slope = [[NSMutableArray alloc] init]; 


} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
[super viewDidDisappear:animated]; 
} 


#pragma mark - Table view data source 

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

// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return 5; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    if ([indexPath section] == 0) { 

     if ([indexPath row] == 0) { 
      UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
      playerTextField.adjustsFontSizeToFitWidth = YES; 
      playerTextField.textColor = [UIColor blackColor]; 
      playerTextField.placeholder = @"Required"; 
      playerTextField.keyboardType = UIKeyboardTypeDefault; 
      playerTextField.returnKeyType = UIReturnKeyNext; 
      playerTextField.text = [course objectAtIndex:indexPath.row]; 
      playerTextField.backgroundColor = [UIColor whiteColor]; 
      playerTextField.textAlignment = UITextAlignmentRight; 
      [playerTextField setEnabled: YES]; 
      [cell addSubview:playerTextField]; 
      [playerTextField release]; 
     } 
     else if([indexPath row] == 1){ 
      UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
      playerTextField.adjustsFontSizeToFitWidth = YES; 
      playerTextField.textColor = [UIColor blackColor]; 
      playerTextField.placeholder = @"Required"; 
      playerTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
      playerTextField.returnKeyType = UIReturnKeyDone; 
      playerTextField.text = [date objectAtIndex:indexPath.row]; 
      playerTextField.backgroundColor = [UIColor whiteColor]; 
      playerTextField.textAlignment = UITextAlignmentRight; 
      [playerTextField setEnabled: YES]; 
      [cell addSubview:playerTextField]; 
      [playerTextField release]; 
     } 
     else if([indexPath row] == 2){ 
      UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
      playerTextField.adjustsFontSizeToFitWidth = YES; 
      playerTextField.textColor = [UIColor blackColor]; 
      playerTextField.placeholder = @"Required"; 
      playerTextField.keyboardType = UIKeyboardTypeNumberPad; 
      playerTextField.returnKeyType = UIReturnKeyDone; 
      playerTextField.text = [scores objectAtIndex:indexPath.row]; 
      playerTextField.backgroundColor = [UIColor whiteColor]; 
      playerTextField.textAlignment = UITextAlignmentRight; 
      [playerTextField setEnabled: YES]; 
      [cell addSubview:playerTextField]; 
      [playerTextField release]; 
     } 
     else if([indexPath row] == 3){ 
      UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
      playerTextField.adjustsFontSizeToFitWidth = YES; 
      playerTextField.textColor = [UIColor blackColor]; 
      playerTextField.placeholder = @"Required"; 
      playerTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
      playerTextField.returnKeyType = UIReturnKeyDone; 
      playerTextField.text = [rating objectAtIndex:indexPath.row]; 
      playerTextField.backgroundColor = [UIColor whiteColor]; 
      playerTextField.textAlignment = UITextAlignmentRight; 
      [playerTextField setEnabled: YES]; 
      [cell addSubview:playerTextField]; 
      [playerTextField release]; 
     } 
     else if([indexPath row] == 4){ 
      UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)]; 
      playerTextField.adjustsFontSizeToFitWidth = YES; 
      playerTextField.textColor = [UIColor blackColor]; 
      playerTextField.placeholder = @"Required"; 
      playerTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
      playerTextField.returnKeyType = UIReturnKeyDone; 
      playerTextField.text = [slope objectAtIndex:indexPath.row]; 
      playerTextField.backgroundColor = [UIColor whiteColor]; 
      playerTextField.textAlignment = UITextAlignmentRight; 
      [playerTextField setEnabled: YES]; 
      [cell addSubview:playerTextField]; 
      [playerTextField release]; 
     } 

    } 
} 
if ([indexPath section] == 0) { 
    if ([indexPath row] == 0) { 
     cell.textLabel.text = @"Course"; 
    } 
    else if([indexPath row] == 1){ 
     cell.textLabel.text = @"Date"; 
    } 
    else if([indexPath row] == 2){ 
     cell.textLabel.text = @"Score"; 
    } 
    else if([indexPath row] == 3){ 
     cell.textLabel.text = @"Rating"; 
    } 
    else if([indexPath row] == 4){ 
     cell.textLabel.text = @"Slope"; 
    } 
} 

return cell;  
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField { 
[course replaceObjectAtIndex:textField.tag withObject:textField.text]; 
[date replaceObjectAtIndex:textField.tag withObject:textField.text]; 
[scores replaceObjectAtIndex:textField.tag withObject:textField.text]; 
[rating replaceObjectAtIndex:textField.tag withObject:textField.text]; 
[slope replaceObjectAtIndex:textField.tag withObject:textField.text]; 
return YES; 
} 
+0

有沒有一個特定的問題,你正在或正在工作,但你只是想放心,你正在使用這種設計的最佳做法? – Dancreek

+0

我只是想確保這是設計應用程序的最佳方式。我不知道如何設置第二個視圖控制器,將元素添加到數組中並將它們顯示在列中。 – user1034642

回答

0

將5個文本字段放在視圖中是不是更容易?我不明白你爲什麼把它們放在一個表格視圖中......如果你希望它們是可滾動的,你總是可以在滾動視圖中包裝你的整個視圖,但是我不會用表格來做到這一點。據我所知,使用表視圖進行輸入通常是不好的。這更像是爲用戶提供一些數據的顯示,例如一個對象數組。此外,通常,如果您使用NSMutableArray作爲數據源,則還希望表格數據源方法(如tableView:numberOfRowsInSection:)根據數組大小返回一個值,而不是一個神奇的硬編碼數字。

我也看到你在代碼中做了很多工作。您可以讓Interface Builder爲您完成所有這些工作:只需創建一個新的UIViewController子類,並使用一個名稱相同的xib,在Interface Builder中進行設計,然後將該邏輯寫入.m文件。

例如,假設我有UIViewController子類,稱爲MyViewController,這與1個按鈕「下一個」和2個UITextField S,通稱爲dateTFscoreTF視圖。我的.h文件看起來是這樣的:

#import <UIKit/UIKit.h> 

@interface MyViewController : UIViewController { 
    IBOutlet UITextField *dateTF; 
    IBOutlet UITextField *scoreTF; 
} 

@property (nonatomic, retain) UITextField *dateTF; 
@property (nonatomic, retain) UITextField *scoreTF; 

- (IBAction)clickedNext:(id)sender; 

@end 

下面我們就針對當按下「下一步」按鈕,將被稱爲動作的佔位符,2個出口到文本字段檢索數據他們。我不需要IBOutlet按鈕,因爲我不會編輯它。

.m文件看起來是這樣的:

#import "MyViewController.h" 

@implementation MyViewController 

@synthesize dateTF 
@synthesize scoreTF; 

// called when the button is clicked 
- (IBAction)clickedNext:(id)sender{ 
    NSString * date = self.dateTF.text; 
    NSString * score = self.scoreTF.text; 
    // do something with this data, like put up a model and go to the next view 
} 

@end 

這是假設我有一個包含我的UI中的廈門國際銀行文件。在xib中,您還需要創建一些連接(使用ctrl + click_and_drag輕鬆實現從源到目標,例如從按鈕到文件所有者):您需要連接2個IBOutlets和該按鈕來調用IBAction,你應該很好走。

希望這會有所幫助!

+0

感謝您的幫助,但我從來沒有學會如何使用界面構建器。我被教導要以一切方式進行。我想我會像你說的那樣放5個文本框。 – user1034642

0

我用數據字典中傳遞數據到一個新的viewController,並取消對代碼中的m:

/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

,並添加新的參數在我的案件的字典:

假設名稱爲passDataHereController和selectedNews在你要去哪裏來傳遞數據控制器聲明的NSDictionary在.H

// Note that I added the withDataDictionary:(NSDictionary*)passedDataDictionary 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withDataDictionary:(NSDictionary*)passedDataDictionary{ 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initialization 
     self.selectedNews = passedDataDictionary; 
    } 
    return self; 
} 

然後

//supposed dataTobePassed is already populated 
    passDataHereController *newView = [[passDataHereController alloc] initWithNibName:@"passDataHereController" bundle:nil withDataDictionary:dataTobePassed]; 
    [self.navigationController pushViewController:newView animated:YES]; 
    [newView release]; 

這是將數據傳遞到另一個視圖的一種方式,其他視圖可能有更簡單的解決方案。 希望它有幫助。