我不确定我是否正确接近这个,所以我需要一些帮助。我有一个包含5行的表格,用于输入数据。用户输入所有数据后,导航栏上会出现一个按钮,用于将数据添加到阵列中。我想设置另一个视图控制器,显示用户输入的所有数据。我希望这是有道理的。这是我到目前为止所拥有的。
- (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 :(得分:0)
在视图中放置5个文本字段会不会更容易?我不明白为什么你把它们放在一个表格视图中...如果你想让它们可滚动,你总是可以将整个视图包装在一个滚动视图中,但我不会用表格来做。据我所知,使用表视图输入通常很糟糕。它更像是一种向用户提供某些数据显示的方法,例如一个对象数组。此外,通常,如果您使用NSMutableArray
作为数据源,您还希望像tableView:numberOfRowsInSection:
这样的表数据源方法根据数组的大小返回值,而不是魔术,硬编码。
我也看到你在代码中做了很多工作。您可以让Interface Builder为您完成所有这些工作:只需创建一个新的UIViewController
子类,其中xib使用相同的名称,在Interface Builder中设计它,然后在.m文件中写入逻辑。
例如,假设我有一个名为UIViewController
的{{1}}子类,其中包含一个按钮'next'和2 MyViewController
s,方便地称为UITextField
}和dateTF
。我的.h文件看起来像这样:
scoreTF
这里我们有一个占位符,用于在按下“下一步”按钮时调用的操作,以及2个文本字段的出口以从中检索数据。我不需要按钮的IBOutlet,因为我不会编辑它。
.m文件可能如下所示:
#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
这假设我有一个包含我的UI的xib文件。在xib中你还需要建立一些连接(使用ctrl + click_and_drag从源到目标轻松完成,例如从按钮到文件的所有者):你需要连接2个IBOutlets和按钮来调用IBAction,你应该好好去。
希望这有帮助!
答案 1 :(得分: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在.h
中声明为NSDictionary// 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];
这是将数据传递到另一个视图的一种方式,其他方法可能有更简单的解决方案。 希望它有所帮助。