我必須在使用tableview的iPhone應用程序中呈現動態表單。此表單可能具有多個UI控件,如按鈕,文本框,標籤,選取器,日期選擇器。在iPhone應用程序中呈現動態表單
我得到了一個能夠呈現動態表單的iPad示例代碼,但this示例代碼使用的是UIPopoverController,這是iPhone應用程序不支持的。 所以我正在尋找一些應該在iPhone中工作的示例代碼。
下面是在單擊動態表單中的按鈕時顯示組合框的代碼。我需要應該顯示選擇器與列表項的代碼。
示例代碼:對git的樞紐Popover for iPhone可用https://github.com/ecrichlow/iPad-Dynamic-Table-Cells
- (IBAction)buttonPressed:(id)sender
{
[delegate rowItemWasSelected:self];
if (self.itemControlType == ControlTypeToggleButton)
{
...
}
else if (self.itemControlType == ControlTypePopup)
{
...
}
else if (self.itemControlType == ControlTypeCombo)
{
UITableViewController *popoverTable = [[[UITableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, DEFAULT_POPOVER_WIDTH, DEFAULT_TOOLBAR_HEIGHT)] autorelease];
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, DEFAULT_POPOVER_WIDTH, ([self.controlSelections count] * popoverTable.tableView.rowHeight) + DEFAULT_TOOLBAR_HEIGHT)] autorelease];
UIViewController *containerViewController = [[[UIViewController alloc] init] autorelease];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:containerViewController];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(DEFAULT_COMBO_TEXTFIELD_MARGIN, (DEFAULT_TOOLBAR_HEIGHT - DEFAULT_COMBO_TEXTFIELD_HEIGHT)/2, DEFAULT_POPOVER_WIDTH - (DEFAULT_COMBO_TEXTFIELD_MARGIN * 2), DEFAULT_COMBO_TEXTFIELD_HEIGHT)];
textField.delegate = self;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.font = [UIFont systemFontOfSize:DEFAULT_COMBO_FONT_SIZE];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[textField addTarget:self action:@selector(fieldTextDidUpdate:) forControlEvents:UIControlEventEditingDidEnd];
containerViewController.view = containerView;
popoverTable.tableView.dataSource = self;
popoverTable.tableView.delegate = self;
popoverTable.tableView.frame = CGRectMake(0, DEFAULT_TOOLBAR_HEIGHT, DEFAULT_POPOVER_WIDTH, [self.controlSelections count] * popoverTable.tableView.rowHeight);
popoverController.popoverContentSize = CGSizeMake(DEFAULT_POPOVER_WIDTH, ([self.controlSelections count] * popoverTable.tableView.rowHeight) + DEFAULT_TOOLBAR_HEIGHT);
popoverController.delegate = self;
[toolbar addSubview:textField];
[containerView addSubview:toolbar];
[containerView addSubview:popoverTable.tableView];
optionPopoverController = popoverController;
[popoverController presentPopoverFromRect:control.frame inView:control.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// If there's currently an object that's first responder, make it resign that status
for (UIView *subview in self.control.superview.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
if ([subview isFirstResponder])
{
[subview resignFirstResponder];
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
}
}
[textField becomeFirstResponder];
}
else if (self.itemControlType == ControlTypeButton)
{
// Don't need to do anything here. Caller passed in target and action. But in order to trigger delegate rowItemWasSelected this control type was added here.
}
}
爲什麼不ü接受的答案?如果你不接受,那麼沒有人會表示有興趣回答你的問題。 –