【问题标题】:Odd behavior UIActionSheet + UITableView: rows disappear奇怪的行为 UIActionSheet + UITableView:行消失
【发布时间】:2013-05-28 08:46:03
【问题描述】:

我有一个 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 可以从 Core Data 中正常填充。那么问题出在哪里?关键是当任何一行被点击时,整个表格都会变空(我还不能发布图片,抱歉)。

我尝试添加一个带有按钮的新自定义行,该按钮会触发向 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

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

问候。

佩德罗·文图拉。

【问题讨论】:

  • 是因为placesView托管对象上下文和placesView = nil吗?
  • @verbumdei:我认为不是,UITableView 在两种解决方案(UIActionSheet 内部和外部)中都显示来自 Core Data 的数据。

标签: iphone ios core-data uitableview uiactionsheet


【解决方案1】:

你可以使用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 的方法来选择按钮:

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

         NSLog(@"index==>%d",buttonIndex);
           NSLog(@"cancel");
    }

【讨论】:

  • 我做到了,在 UIActionSheet 中不会触发此事件。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"Selected row %@", indexPath); }
  • shouldHighlightRowAtIndexPath、didSelectRowAtIndexPath 和 didSelectRowAtIndexPath 在使用 UIActionSheet 时不会被调用;但是是的,这些其他的:numberOfSectionsInTableView、sectionIndexTitlesForTableView、numberOfRowsInSection、cellForRowAtIndexPath。
猜你喜欢
  • 2010-11-14
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多