UIAtionSheet 与 UITableView

发布于 2024-11-03 02:46:26 字数 616 浏览 1 评论 0原文

我需要一些关于 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) 单击“完成”按钮时如何获取所选行?

有什么建议吗?

谢谢..

I need some suggestions about UIActionSheet. In my case I have created an ActionSheet in which I am displaying the UITableView with some data.

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];

the CustomView contains the table . Data is displaying properly, it displays exact 3 rows, now I want some similar functionality like :
(1) when I click on any row, the selected row should come at center.
(2) and where should I add buttons (done , cancel) in CustomView or in ActionSheet ?
(3) and how I get the selected row when I click on Done button?

any suggestions ?

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2024-11-10 02:46:26

ActionSheetDataSource.h

#import <UIKit/UIKit.h>

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

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

@end

CustomActionSheet.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

customActionsheet.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

CustomActionSheetView.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];
    }

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

exampleview.h

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

<CustomActionSheetDelegate>

exampleview.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;
     }

CustomButton.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

ActionSheetDataSource.h

#import <UIKit/UIKit.h>

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

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

@end

CustomActionSheet.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

customActionsheet.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

CustomActionSheetView.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];
    }

Now you can call all this from any view as below

exampleview.h

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

<CustomActionSheetDelegate>

exampleview.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;
     }

CustomButton.h
#import

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

CustomButton.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
南烟 2024-11-10 02:46:26

(1)

当我单击任何行时,所选行应位于中心。

请向我提供有关此问题的更多详细信息,我无法解决。

(2)

我应该在 CustomView 或 ActionSheet 中的哪里添加按钮(完成、取消)?

将它们添加到 UIActionSheet 中:

 UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self     cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button"     otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
 popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
 [popupQuery showInView:self.view];
 [popupQuery release];

(3)

单击“完成”按钮时获取所选行?

NSIndexPath *indexPath = [MyTableView indexPathForSelectedRow] 

(1)

when I click on any row, the selected row should come at center.

Kindly provide me with more details on this issue i can't get it right.

(2)

and where should I add buttons (done , cancel) in CustomView or in ActionSheet ?

Add them in the UIActionSheet:

 UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self     cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button"     otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
 popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
 [popupQuery showInView:self.view];
 [popupQuery release];

(3)

get the selected row when I click on Done button?

NSIndexPath *indexPath = [MyTableView indexPathForSelectedRow] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文