iOS-如何从UITableViewCell显示不同的场景
我有一个iOS-如何从UITableViewCell显示不同的场景,ios,objective-c,uiviewcontroller,tableview,tableviewcell,Ios,Objective C,Uiviewcontroller,Tableview,Tableviewcell,我有一个UIViewController和一个TableView。TableView为每个单元格填充图像和文本。现在,当用户点击一个单元格时,我需要呈现其他场景。例如: -点击第一行-->显示ViewController1-点击第二行-->显示ViewController2 ViewController1和ViewController2是我的故事板中的场景 我尝试过各种解决方案,但都不管用。此外,当我点击单元格时(例如,我试图显示警报),似乎没有调用方法didSelectRowAtIndexP
UIViewController
和一个TableView
。TableView
为每个单元格填充图像和文本。现在,当用户点击一个单元格时,我需要呈现其他场景。例如:
-点击第一行-->显示ViewController1
-点击第二行-->显示ViewController2
ViewController1
和ViewController2
是我的故事板中的场景
我尝试过各种解决方案,但都不管用。此外,当我点击单元格时(例如,我试图显示警报),似乎没有调用方法didSelectRowAtIndexPath:
下面是我的ViewController的代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
我是iOS开发新手,所以我不知道我的代码是否正确,或者是否有其他更优雅的解决方案。不管怎样,让我们把重点放在这个问题上,有人能帮我吗 根据您的代码,仅当您单击第一个单元格时,才会显示警报。。
但是根据你的需要,让我为你写一个代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// here we get the cell from the selected row.
NSString *selectedcelltext=[recipes objectAtIndex:indexPath.row];
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:@"Row Selected" message:[NSString stringWithFormat:@"You've selected a %@ row ",selectedcelltext] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
if ([selectedcelltext isEqualToString:recipes[0]]) {
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"news"];
[self presentViewController:vc animated:YES completion:nil];
}
else if ([selectedcelltext isEqualToString:recipes[1]]) {
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"calendar"];
[self presentViewController:vc animated:YES completion:nil];
}
// use the image in the next window using view controller instance of that view.
}
此外,当我点击一个单元格时(例如,我试图显示一个警报),似乎没有调用didselectrowatinexpath:方法
您的基础类是UIViewController
,因此,UITableViewDelegate
和UITableViewDataSource
不会自动设置。你需要自己做。例如,您可以在init
函数中设置它们:
- (instancetype)init {
self = [super init];
if( self ) {
self.delegate = self;
self.datasource = self;
}
return self;
}
另一种方法是使用UITableViewController
作为基本调用,这样就不必担心如何设置代理 如果您的视图只是一个表视图,我建议使用UITableViewController而不是UIViewController。如果现有UIViewController中有UITableView,则需要自己设置委托和数据源方法。
您可以在故事板中执行此操作。单击tableView,然后选择连接检查器。然后单击数据源旁边的圆圈并将其拖动到视图控制器。对代理执行相同的操作。这可能就是没有调用表视图方法的原因
如果它是一个静态表,您可以从故事板中的每个单元格创建独立的分段 由于您在单元格上添加了图像,因此未调用您的didselectedRowatinedExath
。请设置imageView单元格的属性。imageView.userInteractionEnabled=false。在此之后,您的选择了OWATINDEXPATH
调用。还可以设置委托和数据源,如self.tabelView.Delegate=self;self.tableView.datasource=self代码>@Kurtis92,在.m文件中,您可以将它们放在viewDidLoad函数…facepalm之前。我知道这一点,但我忘了将代理拖动到视图控制器。多么愚蠢的错误啊。谢谢你提醒我这件事。也许这里的每个人都想这么说,但你的回答让我明白发生了什么。
- (instancetype)init {
self = [super init];
if( self ) {
self.delegate = self;
self.datasource = self;
}
return self;
}