我有一个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触发push segue;这工作正常,行为是预期的。所以问题出在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
我被困住了。我做错了什么?
问候。
Pedro Ventura。
答案 0 :(得分: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的方法:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"index==>%d",buttonIndex);
NSLog(@"cancel");
}