iOS - 如何从 UITableViewCell 呈现不同的场景

标签 ios objective-c uiviewcontroller tableview uitableview

我有一个带有 TableViewUIViewControllerTableView 为每个单元格填充了一个图像和一个文本。现在我需要在用户点击单元格时向其他人展示 Scenes。例如:


- 点击第一行 --> 显示 ViewController1
- 点击第二行 --> 显示 ViewController2


ViewController1ViewController2 是我的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/

相关文章:

ios - 我可以在 ios 中手动实现本地化吗?

iphone - 在 iOS 中提取 MP3 专辑插图

iphone - 在 Objective-c 中,如果结构没有初始化,它的成员返回什么值?

ios - 当我的 UIViewController 在我的 Swift 应用程序中显示给用户时,如何检查它是否被查看?

ios - 如何向 ViewController 添加填充/边距

ios - 无法从 iTunes 下载图像作品

ios - 如何在 Swift if 语句中捕获 0 的返回值?

ios - 协议(protocol)/委托(delegate)不起作用

ios - 带有 Storyboard ios 的 ScrollView

IOS - 第二次启动我的应用程序在后台自动运行