我不知道如果我正在接近這個正確的方式,所以我需要一些幫助。我有一個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;
}
有沒有一個特定的問題,你正在或正在工作,但你只是想放心,你正在使用這種設計的最佳做法? – Dancreek
我只是想確保這是設計應用程序的最佳方式。我不知道如何設置第二個視圖控制器,將元素添加到數組中並將它們顯示在列中。 – user1034642