iOS - 如何从 UITableViewCell 呈现不同的场景
iOS - How to present different scenes from UITableViewCell
我有一个 UIViewController
和一个 TableView
。 TableView
为每个单元格填充了图像和文本。现在我需要在用户点击一个单元格时显示其他人 Scenes
。例如:
- 点击第一行 --> 显示 ViewController1
- 点击第二行 --> 显示 ViewController2
ViewController1
和ViewController2
是我Storyboard
中的场景。
我尝试了各种解决方案,但 none 这些解决方案都有效。此外,当我点击一个单元格时,似乎没有调用方法 didSelectRowAtIndexPath:
(例如,我试图显示一个警报)。
这是我的 ViewController 的代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
NSArray *recipes;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
recipes = [NSArray arrayWithObjects:@"News", @"Calendar",@"Take a photo",@"Draw",@"Mail us",@"Follow us on Facebook", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [recipes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:recipes[0]]) {
cell.imageView.image = [UIImage imageNamed:@"newsicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[1]]) {
cell.imageView.image = [UIImage imageNamed:@"mensaicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[2]]) {
cell.imageView.image = [UIImage imageNamed:@"fotoicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[3]]) {
cell.imageView.image = [UIImage imageNamed:@"drawicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[4]]) {
cell.imageView.image = [UIImage imageNamed:@"mailicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[5]]) {
cell.imageView.image = [UIImage imageNamed:@"fbicon"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// here we get the cell from the selected row.
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell.textLabel.text isEqualToString:recipes[0]]) {
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"news"];
[self presentViewController:vc animated:YES completion:nil];
}
// use the image in the next window using view controller instance of that view.
}
@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.
}
Moreover it seems that the method didSelectRowAtIndexPath: is not called when I tap a cell (for example I tried to show up an alert).
您的基础 class 是 UIViewController
,因此,您的 UITableViewDelegate
和 UITableViewDataSource
不会自动设置。你需要自己做。例如,您可以在 init
函数中设置它们:
- (instancetype)init {
self = [super init];
if( self ) {
self.delegate = self;
self.datasource = self;
}
return self;
}
另一种方法是使用 UITableViewController
作为基准调用,这样您就无需担心设置委托。
如果您的视图只是一个 table 视图,我建议您使用 UITableViewController 而不是 UIViewController。如果您在现有的 UIViewController 中有一个 UITableView,您需要自己设置委托和数据源方法。
您可以在情节提要中执行此操作。单击您的 tableView,然后单击 select 连接检查器。然后单击 dataSource 旁边的圆圈并将其拖动到视图控制器。对委托做同样的事情。这可能是您的 table 视图方法未被调用的原因。
如果它是静态的 table,您可以从情节提要中的每个单元格创建独立的转场。
我有一个 UIViewController
和一个 TableView
。 TableView
为每个单元格填充了图像和文本。现在我需要在用户点击一个单元格时显示其他人 Scenes
。例如:
- 点击第一行 --> 显示 ViewController1
- 点击第二行 --> 显示 ViewController2
ViewController1
和ViewController2
是我Storyboard
中的场景。
我尝试了各种解决方案,但 none 这些解决方案都有效。此外,当我点击一个单元格时,似乎没有调用方法 didSelectRowAtIndexPath:
(例如,我试图显示一个警报)。
这是我的 ViewController 的代码: ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
NSArray *recipes;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
recipes = [NSArray arrayWithObjects:@"News", @"Calendar",@"Take a photo",@"Draw",@"Mail us",@"Follow us on Facebook", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [recipes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:recipes[0]]) {
cell.imageView.image = [UIImage imageNamed:@"newsicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[1]]) {
cell.imageView.image = [UIImage imageNamed:@"mensaicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[2]]) {
cell.imageView.image = [UIImage imageNamed:@"fotoicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[3]]) {
cell.imageView.image = [UIImage imageNamed:@"drawicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[4]]) {
cell.imageView.image = [UIImage imageNamed:@"mailicon"];
}
else if ([cell.textLabel.text isEqualToString:recipes[5]]) {
cell.imageView.image = [UIImage imageNamed:@"fbicon"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// here we get the cell from the selected row.
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell.textLabel.text isEqualToString:recipes[0]]) {
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"news"];
[self presentViewController:vc animated:YES completion:nil];
}
// use the image in the next window using view controller instance of that view.
}
@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.
}
Moreover it seems that the method didSelectRowAtIndexPath: is not called when I tap a cell (for example I tried to show up an alert).
您的基础 class 是 UIViewController
,因此,您的 UITableViewDelegate
和 UITableViewDataSource
不会自动设置。你需要自己做。例如,您可以在 init
函数中设置它们:
- (instancetype)init {
self = [super init];
if( self ) {
self.delegate = self;
self.datasource = self;
}
return self;
}
另一种方法是使用 UITableViewController
作为基准调用,这样您就无需担心设置委托。
如果您的视图只是一个 table 视图,我建议您使用 UITableViewController 而不是 UIViewController。如果您在现有的 UIViewController 中有一个 UITableView,您需要自己设置委托和数据源方法。
您可以在情节提要中执行此操作。单击您的 tableView,然后单击 select 连接检查器。然后单击 dataSource 旁边的圆圈并将其拖动到视图控制器。对委托做同样的事情。这可能是您的 table 视图方法未被调用的原因。
如果它是静态的 table,您可以从情节提要中的每个单元格创建独立的转场。