【问题标题】:UITableView segue not workingUITableView segue 不工作
【发布时间】:2013-04-24 02:24:40
【问题描述】:

我按照文档中的 SimpleDrillDown 应用示例创建了一个锻炼应用,该应用在第一个 UITableView 中显示锻炼名称,在第二个 UITableView 中显示锻炼。

我的应用在 Dropbox 中:http://db.tt/V0EhVcAG

我使用了故事板,有原型单元格,但是当我在模拟器中加载时,第一个 UITableView 不会让我点击并转到详细的 UITableView。披露指标 V 形不加载。应用构建成功,没有正式错误。

我的 tableviews 在导航控制器中,我的 segue 和原型单元格都在故事板中相应地命名。

SpitfireViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataController countOfList];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"WorkoutCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Workout *workoutAtIndex = [dataController objectInListAtIndex:indexPath.row];
    cell.textLabel.text = workoutAtIndex.title;

    return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showExercises"]) {
        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];

        DetailViewController *detailViewController = [segue destinationViewController];

        detailViewController.workout = [dataController objectInListAtIndex:selectedRowIndex.row];
    }
}

DetailViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [workout.exercises count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ExerciseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [workout.exercises objectAtIndex:indexPath.row];
    return cell;
}

AppDelegate.h

#import <UIKit/UIKit.h>
@class DataController;
@class SpitfireViewController;

@interface SpitfireAppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    SpitfireViewController *spitfireViewController;
    DataController *dataController;
}

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

#import "SpitfireAppDelegate.h"
#import "SpitfireViewController.h"
#import "DataController.h"

@implementation SpitfireAppDelegate
@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    spitfireViewController = [[SpitfireViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:spitfireViewController];

    DataController *controller = [[DataController alloc] init];
    spitfireViewController.dataController = controller;

    [window addSubview:[navController view]];
    [self.window makeKeyAndVisible];

    return YES;
}

@end

【问题讨论】:

  • 请将您的项目上传到某个地方并链接到它。

标签: ios uitableview segue


【解决方案1】:

如果您有 tableview 单元格 segue 选择问题。

  • 您需要先检查您的表格视图是否允许选择
  • 使用“静态单元格”时无需实现UITableViewDelegateUITableViewDataSource
  • 您需要为附属动作添加一个转场 + 为选择添加一个转场。

【讨论】:

    【解决方案2】:

    使用故事板时,您通常会在信息选项卡下的项目设置中设置故事板文件名。一旦你在那里选择了你的故事板,你基本上可以删除除了application:didFinishLaunchingWithOptions: 方法的return YES 部分之外的所有内容。这一切都在情节提要中为您处理好了。

    编辑:

    这里是你设置故事板的地方:

    还要确保您的视图控制器设置为初始视图控制器:

    【讨论】:

    • 嘿 Ben - 当我这样做时,我看不到我的桌面视图。导航栏加载,但 tableview 是黑色的。
    • 为了清晰起见,我添加了一些屏幕截图。这是你的吗?
    • 我下载了你的项目。问题是从情节提要加载时您的表格视图为零。如果您删除并重新将 tableview 控制器拖到情节提要上,然后从导航控制器 ctrl-drag 到新的 table view 控制器,将其设置为根视图控制器并运行它,您将看到 table view。然后你可以设置类,配置原型单元等。我认为问题是你从一个 UIViewController 开始并将一个表格视图拖到它上面。
    • 明白了!是的,我记得将 tableview 拖到 UIViewController 上。我像你说的那样重新做了我的故事板,并删除了 AppDelegate 中的代码。我的 tableview 加载并且不再是黑色的,但是单元格没有内容,目前正在尝试解决这个问题。 :P
    • 我的回答还提到了清理application:didFinishLaunchingWithOptions: 方法;)
    【解决方案3】:

    我认为这是你的问题:

        spitfireViewController = [[SpitfireViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:spitfireViewController];
    
        DataController *controller = [[DataController alloc] init];
        spitfireViewController.dataController = controller;
    

    您在此处创建的 spitfireViewController 不是您故事板中的那个,而是一个新的。您应该删除所有这些代码,因为您已经在故事板中制作的导航控制器中嵌入了一个 spitfireViewController。你应该在它的 viewDidLoad 方法中为 spitfireViewController 设置数据控制器:

        DataController *controller = [[DataController alloc] init];
        self.dataController = controller;
    

    【讨论】:

    • 非常感谢!现在有道理了。这与@Ben 的回答结合使用。
    【解决方案4】:

    您的“didSelectRowAtIndexPath”方法在哪里?

    我建议将其放入然后触发 [self performSegueWithIdentifier:] 以使用表格单元格中的数据作为发送者来触发 segue。

    【讨论】:

    • 这不对。如果您使用故事板,则根本不使用didSelectRowAtIndexPath
    • @BenScheirman 所以我遇到了类似的问题。您如何处理点击单元格以呈现详细的表格视图?
    • 通常你会定义一个从一个单元到另一个视图控制器的segue,然后为你处理转换。如果你需要为新的视图控制器提供状态,你可以在 prepareForSegue 中完成
    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 2014-05-23
    • 2014-07-21
    • 1970-01-01
    • 2015-04-24
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多