iphone - UIAtionSheet 与 UITableView

标签 iphone uitableview uiactionsheet

我需要一些关于 UIActionSheet 的建议。就我而言,我创建了一个 ActionSheet,在其中显示带有一些数据的 UITableView。

UIActionSheet *asheet = [[UIActionSheet alloc] init];
[asheet showInView:self.view]; 
[asheet setFrame:CGRectMake(0, 250, 320, 320)];


CustomView *innerView = [[CustomView alloc] initWithNibName:@"CustomView" bundle:nil];
innerView.view.frame = CGRectMake(0, 10, 320, 210);

[asheet addSubview:innerView.view];

CustomView 包含表格。数据显示正确,它显示精确的 3 行,现在我想要一些类似的功能,例如:
(1) 当我单击任何行时,所选行应位于中心。
(2) 我应该在 CustomView 或 ActionSheet 中的哪里添加按钮(完成、取消)?
(3) 当我单击“完成”按钮时如何获取所选行?

有什么建议吗?

谢谢..

最佳答案

ActionSheetDataSource.h

#import <UIKit/UIKit.h>

@interface ActionSheetDataSource:NSObject
{
NSMutableArray*otherButtonArr;
NSString*title;
}

@property(nonatomic,strong) NSMutableArray*otherButtonArr;
@property(nonatomic,strong) NSString*title;

@end

自定义操作表.h

#import <UIKit/UIKit.h>
#import "CustomActionSheetView.h"
#import "ActionSheetDataSource.h"
#import "CustomButton.h"

 @interface CustomActionSheet : UIViewController <UITableViewDataSource, UITableViewDelegate>
 {
     id<CustomActionSheetDelegate> delegate;
    ActionSheetDataSource*dataSource;
//        ActionsheetType TYPE;
    IBOutlet UITableView*tblView;
 }

@property (nonatomic,retain)IBOutlet UITableView*tblView;
@property (nonatomic,retain)id<CustomActionSheetDelegate> delegate;
@property (nonatomic,retain)ActionSheetDataSource*dataSource;
//    @property (nonatomic)ActionsheetType TYPE;

-(IBAction)actionBtnExit:(UIButton*)sender;

@end

自定义Actionsheet.m

@synthesize delegate;
@synthesize dataSource;
//@synthesize TYPE;
@synthesize tblView;


  #pragma mark - Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[self dataSource] title];
}

- (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 [[self.dataSource otherButtonArr] count];
  }

- (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];
}
if ([[cell contentView] viewWithTag:25]) 
{
    [[[cell contentView] viewWithTag:25] removeFromSuperview];
}


    CustomButton*cButton= [[[self dataSource] otherButtonArr] objectAtIndex:indexPath.row];
    cell.textLabel.text = [cButton titleForState:UIControlStateNormal];
    cell.textLabel.textColor = [cButton titleColorForState:UIControlStateNormal];
    cell.accessoryType =   (cButton.selected)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;


  return cell;
  }

  #pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
CustomButton*cButton= [[[self dataSource] otherButtonArr] objectAtIndex:indexPath.row];
[self didSelectedOption:[cButton titleForState:UIControlStateNormal]];
}

-(void)hideAction
{
[[self view] removeFromSuperview];
}

-(void)actionBack
{
[self didSelectedOption:@"Go Back"];
}

-(IBAction)actionBtnExit:(UIButton*)sender
{
[self didSelectedOption:@"Go Back"];
}

-(void)didSelectedOption:(NSString*)option
{
if ([[self delegate] respondsToSelector:@selector(didSelectedOption:)]) 
{
    [[self delegate] didSelectedOption:option];
}
CGRect frame=[[self view] frame];
frame.origin =CGPointMake(frame.origin.x,frame.size.height+frame.origin.y);
[UIView beginAnimations:@"hideCustomActionSheetView" context:nil];  
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView  setAnimationDidStopSelector:@selector(hideAction)];
[[self view] setFrame:frame];
[UIView commitAnimations];
}

CustomActionSheetView.h

#import <Foundation/Foundation.h>
#import "ActionSheetDataSource.h"

typedef enum{
    ACTIONS,
    OPTIONS
}ActionsheetType;

@class CustomActionSheet;
@protocol CustomActionSheetDelegate;

@interface CustomActionSheetView : NSObject
{
}

+(void)loadActionSheetInView:(UIView*)view withDataSource:   (ActionSheetDataSource*)dataSource andDelegate:(id<CustomActionSheetDelegate>)delegate;
+(void)loadActionSheetInView:(UIView*)view withDataSource:(ActionSheetDataSource*)dataSource delegate:(id<CustomActionSheetDelegate>)delegate andType:(ActionsheetType)TYPE;
@end

@protocol CustomActionSheetDelegate <NSObject>
@required
-(void)didSelectedOption:(NSString*)option;
@end

自定义ActionSheetView.m

    +(void)loadActionSheetInView:(UIView*)view withDataSource:(ActionSheetDataSource*)dataSource andDelegate:(id<CustomActionSheetDelegate>)delegate
    {
    CustomActionSheet*actionSheet= [[CustomActionSheet alloc] initWithNibName:@"CustomActionSheet" bundle:nil];
    [actionSheet setDelegate:delegate];
    [actionSheet setDataSource:dataSource];
    CGRect frame=[view frame];
    frame.size.height -= 20;
    frame.origin =CGPointMake(frame.origin.x,frame.size.height+frame.origin.y);
    [[actionSheet view] setFrame:frame];
    [[actionSheet tblView] reloadData];
    [view addSubview:[actionSheet view]];
    [UIView beginAnimations:@"RS_showKeyboardAnimation" context:nil];   
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3];
    CGRect frame2=[view frame];
    frame2.size.height -= 20;
    frame2.origin =CGPointMake(frame2.origin.x,10+frame2.origin.y);
    [[actionSheet view] setFrame:frame2];
    [UIView commitAnimations];
    }

现在您可以从任何 View 调用所有这些,如下所示

示例 View .h

#import "ActionSheetDataSource.h"
#import "CustomActionSheet.h"

<CustomActionSheetDelegate>

示例 View .m

 -(void)calling
    {
        ActionSheetDataSource*dataSource=[[[ActionSheetDataSource alloc] init] autorelease];//mm
    [dataSource setTitle:@"I want to"];
    CustomButton*cBack=[CustomButton CustomButtonWithColor:[UIColor blackColor] andTitle:@"Go Back"];
   NSMutableArray *arr = [NSMutableArray arrayWithObjects:cBack ,nil];
    [dataSource setOtherButtonArr:arr];
    [CustomActionSheetView loadActionSheetInView:self.view withDataSource:dataSource andDelegate:self];

    }

    -(void)didSelectedOption:(NSString*)option
    {
     if([option isEqualToString:@"Go Back"])
        return;
     }

自定义按钮.h #导入

@interface CustomButton : UIButton
+(CustomButton*)CustomButtonWithColor:(UIColor*)color andTitle:(NSString*)title;
+(CustomButton*)CustomButtonWithColor:(UIColor*)color title:(NSString*)title andSelectedState:(BOOL)state;
@end

自定义按钮.m

#import "CustomButton.h"

@implementation CustomButton

+(CustomButton*)CustomButtonWithColor:(UIColor*)color andTitle:(NSString*)title
{
CustomButton*btn=[[self class] buttonWithType:UIButtonTypeCustom];
if (btn) 
{
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
return btn;
}

+(CustomButton*)CustomButtonWithColor:(UIColor*)color title:(NSString*)title andSelectedState:(BOOL)state
{
CustomButton*btn=[[self class] buttonWithType:UIButtonTypeCustom];
if (btn) 
{
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    [btn setSelected:state];
}
return btn;
}
@end

关于iphone - UIAtionSheet 与 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5778216/

相关文章:

iphone - iphone锁屏后应用程序还能继续运行多久?

iphone - iphone 应用商店是否向 url 提供即时付款通知?

ios - accessoryButtonTappedForRowWithIndexPath : not getting called

ipad - UIActionSheet Controller在iPad上崩溃,在iPhone上运行正常

iphone - 处理 UIWebView 内的缩放和平移

iphone - 核心数据不同提取给出空数组

ios - 在 UITableViewCell 中更新 UITextView 的 attributedText 是滞后的

objective-c - 静态 UITableView 不是全屏

ios - Swift iOS - 如何将 UIPopoverBackgroundView 类中的方法连接到不同类中的 PopoverController?

ipad - 更改 iOS8 中的操作表弹出箭头