UIActionSheet与UITableView

3

我需要一些关于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行,现在我希望有一些类似的功能:

  • 当我点击任何一行时,选定的行应该居中显示。
  • 我应该把按钮(完成,取消)放在CustomView还是ActionSheet中?
  • 当我点击“完成”按钮时,如何获取所选行?

有什么建议吗?

谢谢。


你想让你的按钮显示在表格视图还是操作表中? - dks1725
将您的按钮作为操作表的子视图添加。 - dks1725
在按钮点击事件中如何获取所选行? - Maulik
你需要使用flag,当你点击任何一行时,将会调用didSelectRow方法,因此在该方法中,你可以找到该行并将其分配给flag...当你按下“完成”按钮时,获取该flag的值。 - dks1725
2个回答

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

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

现在你可以像下面这样从任何视图中调用所有这些内容。
例如视图.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

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

2

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

在我的表视图中,它显示了确切的3行。如果我选择第3行,则所选行应该位于中心位置,就像2--3(已选择)---4一样。 - Maulik
@Maulik:好的,你需要查看管理行重新排序 - Ahmad Kayyali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接