【问题标题】:how to push a new view controller from UITableView object cell如何从 UITableView 对象单元推送新的视图控制器
【发布时间】:2012-01-02 05:56:31
【问题描述】:

我正在尝试在 Xcode 4.2 上创建一个 UITableView 应用程序。 我只希望每个单元格(例如 Cali)在推送时推送一个新的 ViewController

我遇到的问题是,每当我按下单元格时,它都不会推动新的视图控制器

我的 TableViewController.h

#import <UIKit/UIKit.h>

@interface Adam : UITableViewController 
{
    NSMutableArray *states;
}

@end

我的 Tableviewcontroller.m

#import "Adam.h"
#import "ViewController.h"


@implementation Adam


- (void)viewDidLoad
{
    [super viewDidLoad];

    states = [NSMutableArray arrayWithObjects:
              @"cali",
              @"ohio",
              nil];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

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

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

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

    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    [cellLabel setText:[states objectAtIndex:indexPath.row]];    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([[states objectAtIndex:indexPath.row] isEqual:@"cali"])

    { 
        ViewController *cali = [[ViewController alloc] initWithNibName:@"cali" bundle:nil]; 
         [self.navigationController pushViewController:cali animated:YES];

    }
}

@end

【问题讨论】:

  • 您遇到了什么问题?如果在 didSelectRowAtIndexPath 中设置断点,它会到达那里吗?
  • 抱歉不清楚。但我刚刚更新了我的问题
  • np。那么它是否进入了didSelectRowAtIndexPath 方法?
  • 对不起,我对此有点陌生,你所说的方法是什么意思。我没有在 AppDelegate 中编写任何代码
  • 在 didSelectRowAtIndexPath 中添加一条日志消息或断点,以查看程序是否在您触摸单元格时运行。

标签: uitableview ios5 xcode4.2 pushviewcontroller


【解决方案1】:
cali.title = [states objectAtIndex: [indexPath row]];
[self.navigationController pushViewController:cali animated:YES];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2016-10-22
    相关资源
    最近更新 更多