我有一个包含一些静态单元格的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和一个Close按钮。
- (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之间。
下面是两个视图控制器的接口:
Main。
@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
我被卡住了。我做错了什么?
致以问候。
佩德罗·文图拉。
发布于 2013-05-28 20:18:56
您可以使用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];
}
}
你可以对Selected Button使用UIActionsheetDelegate的方法:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"index==>%d",buttonIndex);
NSLog(@"cancel");
}
https://stackoverflow.com/questions/16787678
复制相似问题