开发者

UIAtionSheet with UITableView

开发者 https://www.devze.com 2023-02-28 18:52 出处:网络
I need some suggestions about UIActionSheet. In my case I have created an ActionSheet in开发者_StackOverflow社区 which I am displaying the UITableView with some data.

I need some suggestions about UIActionSheet. In my case I have created an ActionSheet in开发者_StackOverflow社区 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..


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


(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] 
0

精彩评论

暂无评论...
验证码 换一张
取 消