Невозможно выдвинуть новый контроллер представления из ячейки UITableView - PullRequest
0 голосов
/ 08 января 2012

Я пытаюсь создать приложение UITableView на Xcode 4.2. Я просто хочу, чтобы каждая клетка (например, Кали) выдвигала новый UIViewController при касании.

Проблема, с которой я сталкиваюсь, заключается в том, что когда я нажимаю на ячейку, новый контроллер представления не выдвигается.

Когда я ставлю точку останова на методе didSelectRowAtIndexPath, я вижу 5 потоков.

Вот мой код:

#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

1 Ответ

0 голосов
/ 08 января 2012

Во-первых, у TableViewCell есть свойство textLabel, поэтому вам не нужно создавать собственную метку.

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

и есть ли причина, по которой вы отменили выбор ячейки? Попробуйте отменить ее выбор после использования indexPath Andпопробуй:

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

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...