简体   繁体   English

无法从UITableView单元格推送新的视图控制器

[英]Can't push a new view controller from UITableView cell

I am trying to create a UITableView application on Xcode 4.2. 我正在尝试在Xcode 4.2上创建UITableView应用程序。 I just want each cells (ex. Cali) to push a new UIViewController when it's touched. 我只希望每个单元(例如Cali)在触摸时推送一个新的UIViewController

The issue I'm running into is whenever I press the cell it's not pushing the new view controller. 我遇到的问题是,每当我按下单元格时,它都不会推动新的视图控制器。

When I put a break point at the didSelectRowAtIndexPath method, I see 5 threads. 当我在didSelectRowAtIndexPath方法上设置一个断点时,我看到5个线程。

Here's my code: 这是我的代码:

#import <UIKit/UIKit.h> 

@interface Adam : UITableViewController
{
    NSMutableArray *states;
}
@end


#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

First the TableViewCell has a textLabel property so you don't need to create your own label. 首先,TableViewCell具有textLabel属性,因此您无需创建自己的标签。

cell.textLabel.text= [states objectAtIndex:indexPath.row];

and is there a reason your deselecting the cell?Try deselecting it after you use the indexPath And try: 并取消选择单元格是有原因的吗?使用indexPath后请尝试取消选择该单元并尝试:

if([states objectAtIndex:indexPath.row] isEqualToString:@"cali"]];){

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式将数据从 UITableView 推送到查看控制器 - push data from UITableView to view controller programmatically 特定的UITableView单元重定向到safari,不要推送到新的视图控制器 - Specific UITableView cells redirect to safari and don't push to new view controller 将UITableView中的单击单元格链接到新视图控制器的基本步骤 - Basic steps in linking click on cell in UITableView to a new view controller 如何从一个视图推一个新视图controller - How to push a new view controller from a view 选择表格视图单元格时无法推送视图控制器 - Can't push my view controller when I select a table view cell uiTableview单元格未显示在视图控制器中 - uiTableview cell is not displaying in view controller 无法从一个视图控制器推到下一个 - Can't Push from one view controller to the next 无法从Web View Controller加载推入URL - Can't load a push url from my Web View Controller 从UITableView推送详细信息视图仅在单元格中的特定UIImage上? - Push Detail View from UITableView only on a specific UIImage in the cell? 如何从情节提要中的子视图(另一个视图控制器)内的按钮推动新的视图控制器 - How can I push a new View Controller from a button within a subView (another View Controller) in Storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM