我有一个带有 TableView
的 UIViewController
。 TableView
为每个单元格填充了一个图像和一个文本。现在我需要在用户点击单元格时向其他人展示 Scenes
。例如:
- 点击第一行 --> 显示 ViewController1
- 点击第二行 --> 显示 ViewController2
ViewController1
和ViewController2
是我的Storyboard
中的场景。
我尝试了各种解决方案,但都不起作用。此外,当我点击一个单元格时,方法 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 开发的新手,所以我不知道我的代码是否正确,或者是否有其他更优雅的解决方案。无论如何,让我们专注于问题,有人可以帮助我吗?
最佳答案
如果您的 View 只是一个表格 View ,我建议您使用 UITableViewController 而不是 UIViewController。如果您在现有的 UIViewController 中有一个 UITableView,您需要自己设置委托(delegate)和数据源方法。
您可以在 Storyboard 中执行此操作。单击您的 tableView,然后选择连接检查器。然后单击 dataSource 旁边的圆圈并将其拖动到 View Controller 。对委托(delegate)做同样的事情。这可能是您的 TableView 方法未被调用的原因。
如果是静态表格,您可以从 Storyboard 中的每个单元格创建独立的转场。
关于iOS - 如何从 UITableViewCell 呈现不同的场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30545717/