2013-01-24 66 views
0

我必須在使用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. 
     } 

} 
+0

爲什麼不ü接受的答案?如果你不接受,那麼沒有人會表示有興趣回答你的問題。 –

回答

1

您可以修改示例代碼ipad公司動態表-Cells公司在iPhone沿渲染動態形式酥料餅的顯示器也

下面的6個文件項目對混帳樞紐https://github.com/50pixels/FPPopover可添加到您的iPad-動態表-細胞樣本代碼

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中。米

- (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; 
} 
1

該項目將幫助你實現在iPhone應用程序的酥料餅。

您需要做的唯一的事情就是檢查上面的代碼中的設備&相應地執行相應的操作。

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 
} 
相關問題