我必须使用tableview在iPhone应用程序中渲染动态表单。此表单可能有多个UI控件,如按钮,文本字段,标签,选择器,日期选择器。
我有一个能够呈现动态表单的iPad示例代码,但this示例代码正在使用UIPopoverController,iPhone应用程序不支持。 所以我正在寻找一些适用于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.
}
}
答案 0 :(得分:1)
git-hub Popover for iPhone上提供的项目将帮助您在iPhone应用中实现popover。
您唯一需要做的就是检查上述代码中的设备&相应地执行行动。
在项目中添加6个文件
FPPopoverController.h / .m,FPPopoverView.h / m和FPTouchView.h / .m
然后创建两个样本
DemoPopOverTableController.h
#import <UIKit/UIKit.h>
#import "FPPopoverController.h"
@interface DemoPopOverTableController : UITableViewController {
FPPopoverController *popOverController;
NSArray *subCat;
}
-(id)initWithStyle:(UITableViewStyle)style andSubCategory:(NSArray *)subCategories
-(void)setPopOver:(FPPopoverController*)popOver;
@end
和
DemoPopOverTableController.m
#import "DemoPopOverTableController.h"
#import "FPPopoverController.h"
@interface DemoTableController ()
@end
@implementation DemoTableController
-(id)initWithStyle:(UITableViewStyle)style andSubCategory:(NSArray *)subCategories
{
self = [super init];
if (self) {
// Custom initialization
subCat = [NSArray arrayWtihArray:subCategories] ;
}
return self;
}
-(void)setPopOver:(FPPopoverController *)popOver
{
popOverController=popOver;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [subCat count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
NSString *tempCat=[subCat objectAtIndex:indexPath.row];
cell.textLabel.text =tempCat;
return cell ;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"selected Category: %@",[subCat objectAtIndex:indexPath.row]);
[popOverController dismissPopoverAnimated:YES];
}
@end
这不是完整的答案,但您可以在上面的代码中执行此操作
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
//iPhone popover lines of code
DemoPopOverTableController *controller = [[DemoPopOverTableController alloc] initWithStyle:UITableViewStylePlain andSubCategory:subCat];
FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
[controller setPopOver:popover];
[controller release];
popover.delegate = self;
popover.tint = FPPopoverDefaultTint;
popover.arrowDirection = FPPopoverArrowDirectionUp;
popover.contentSize = CGSizeMake(200, 200);
//sender is the UIButton view
[popover presentPopoverFromView:sender];
[popover release];
}
else
{
//the original line of code you have already in your code
}
您可以实现委托方法,以便在触摸表格视图时执行某些操作
- (void)presentedNewPopoverController:(FPPopoverController *)newPopoverController
shouldDismissVisiblePopover:(FPPopoverController*)visiblePopoverController
{
[visiblePopoverController dismissPopoverAnimated:YES];
[visiblePopoverController autorelease];
}
- (void)popoverControllerDidDismissPopover:(FPPopoverController *)popoverController
{
// do something of your choice
}
答案 1 :(得分:1)
您可以修改示例代码iPad-Dynamic-Table-Cells,以便在iPhone中呈现动态表单以及弹出显示
将以下6个git-hub https://github.com/50pixels/FPPopover上可用的项目文件添加到您的iPad-Dynamic-Table-Cells示例代码
FPPopoverController.h / .m,FPPopoverView.h / m和FPTouchView.h / .m
现在修改FPTouchView.m,EditableTableDataRowItem.h和EditableTableDataRowItem.m,如下所示
//In EditableTableDataRowItem.h
@interface EditableTableDataRowItem : UIViewController <UITableViewDataSource, UITableViewDelegate, UIPopoverControllerDelegate, UITextFieldDelegate,FPPopoverControllerDelegate>
{
id<EditableTableDataRowItemDelegate> delegate;
RowItemControlType itemControlType; // So named to denote that this does not correspond to a UIControl
NSArray *controlSelections; // List of items to display for popup control type
NSString *listKey; // If controlSelections array contains NSManagedObjects or NSDictionaries, the key to use to get a string to represent the item
CGSize baseSize; // Default size of the control, with width relative to other items on the row
BOOL resizeable; // Determines whether item can be resized based on row width
UIControl *control; // Standard control particular to the type of row item
UIImage *normalImage; // Used to customize the appearance of any button-based control type
UIImage *selectedImage; // Used to customize the appearance of any button-based control type
CGSize originalBaseSize;
int state;
int selectedIndex;
UIPopoverController *optionPopoverController;
FPPopoverController *popover;
id<FPPopoverControllerDelegate> delegate1;
}
@property(assign) id<EditableTableDataRowItemDelegate> delegate;
@property(assign) id<FPPopoverControllerDelegate> delegate1;
@property(nonatomic, retain) FPPopoverController *popover;
.
.
//在EditableTableDataRowItem.m
中- (IBAction)buttonPressed:(id)sender
{
[delegate rowItemWasSelected:self];
if (self.itemControlType == ControlTypeToggleButton)
{
.
.
.
}
else if (self.itemControlType == ControlTypePopup)
{
UITableViewController *popoverTable = [[[UITableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
popoverTable.tableView.dataSource = self;
popoverTable.tableView.delegate = self;
popover = [[FPPopoverController alloc] initWithViewController:popoverTable];
popover.tint = FPPopoverDefaultTint;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
popover.contentSize = CGSizeMake(300, 500);
}
else {
popover.contentSize = CGSizeMake(200, 300);
}
popover.arrowDirection = FPPopoverArrowDirectionAny;
[popover presentPopoverFromView:sender];
}
.
.
.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[popover dismissPopoverAnimated:YES];
}
//在FPTouchView.m文件中 //需要处理此方法
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *subview = [super hitTest:point withEvent:event];
//To-Do
//
return subview;
}