0

我有一个 UITableView,里面有一些静态单元格。其中一个在触摸时显示 UIActionSheet。这是它的代码:

viewDidLoad:初始化控件。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initializeControls];
}

initializeControls:将手势识别器添加到单元格内的标签以显示 UIActionSheet。

- (void)initializeControls {
    [...]

    // Places Label
    self.placeNamesLabel.userInteractionEnabled = YES;
    tapGesture = \
    [[UITapGestureRecognizer alloc]
     initWithTarget:self action:@selector(didPlaceNamesLabelWithGesture:)];
    [self.placeNamesLabel addGestureRecognizer:tapGesture];
    tapGesture = nil;

    [...]
}

didPlaceNamesLabelWithGesture:初始化 UIActionSheet,从 StoryBoard 中添加一个 UITableView 和一个关闭按钮。

- (void)didPlaceNamesLabelWithGesture:(UITapGestureRecognizer *)tapGesture
{
    // Show UITableView in UIActionView for selecting Place objects.
    placesActionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                      delegate:nil
                                             cancelButtonTitle:nil
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];

    [placesActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect tableFrame = CGRectMake(0, 40, 0, 0);

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    AddArticlesPlacesViewController *placesView = [storyBoard instantiateViewControllerWithIdentifier:@"PlacesForArticle"];
    placesView.managedObjectContext = self.managedObjectContext;
    placesView.view.frame = tableFrame;
    [placesActionSheet addSubview:placesView.view];
    placesView = nil;

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES;
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissPlacesActionSheet:) forControlEvents:UIControlEventValueChanged];
    [placesActionSheet addSubview:closeButton];
    closeButton = nil;

    [placesActionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [placesActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

dismissPlacesActionSheet:关闭 UIActionSheet。

-(void)dismissPlacesActionSheet:(id)sender {
    [placesActionSheet dismissWithClickedButtonIndex:0 animated:YES]; }

一切正常,UITableView 从核心数据中很好地填充。那么问题出在哪里?关键是当任何一行被点击时,整个表格都会变空(我还不能发布图片,抱歉)。

我试图添加一个带有按钮的新自定义行,该按钮会触发向 AddArticlesPlacesViewController 的推送序列;这工作正常,行为符合预期。所以问题出在 UIActionSheet 和 UITableView 之间。

以下是两个视图控制器的接口:

主要的。

@interface AddArticleViewController : UITableViewController <NSFetchedResultsControllerDelegate, [...]>

[...]

@property (weak, nonatomic) IBOutlet UILabel *placeNamesLabel;

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

[...]

@end

次要的。

@interface AddArticlesPlacesViewController : UITableViewController <NSFetchedResultsControllerDelegate>

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@end

我被困住了。我究竟做错了什么?

问候。

佩德罗文图拉。

4

1 回答 1

0

您可以使用 tableview 的委托方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

        NSLog(@"indexpath-->%d",indexPath.row);
        if (indexPath.row==0 || indexPath.row == 2 ) 
        {

            NSLog(@"row %d",indexPath.row);

        }

        if (indexPath.row==1)
     {

            actionsheet=[[UIActionSheet alloc]initWithTitle:@"Sample" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
               [actionsheet showInView:self.view];

        }
    }

你可以使用 UIActionsheetDelegate 的 Selected Button 方法:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {

         NSLog(@"index==>%d",buttonIndex);
           NSLog(@"cancel");
    }
于 2013-05-28T12:18:56.297 回答