Выберите определенную строку в одном из вариантов выбора в табличном представлении - PullRequest
0 голосов
/ 06 октября 2011

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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Пожалуйста, дайте мне несколько советов. Большое спасибо.

Вот мой TableViewController.m

#import "TableViewController.h"
#import "LondonController.h"
#import "NewYorkViewController.h"
#import "ParisViewController.h"
#import "TokyoViewController.h"

@implementation TableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    [tableDataSource release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStyleGrouped];
    [table setDataSource:self];
    [table setDelegate:self];   

    tableDataSource = [[NSMutableArray alloc]init];

NSMutableArray* sec1 = [[NSMutableArray alloc] init];
[sec1 addObject:@"London"];
[sec1 addObject:@"New York"];
[sec1 addObject:@"Paris"];
[sec1 addObject:@"Tokyo"];
[tableDataSource addObject:sec1];
[sec1 release];

NSMutableArray* sec2 = [[NSMutableArray alloc] init];
[sec2 addObject:@"Elton John"];
[sec2 addObject:@"Michael Jackson"];
[sec2 addObject:@"Little Prince"];
[sec2 addObject:@"SMAP"];
[tableDataSource addObject:sec2];
[sec2 release];

[self.view addSubview:table];

[table release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    if ( tableDataSource == nil )
        return 1;
    return [tableDataSource count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{    
    NSInteger bucketCount = -1;
    NSObject *target_section;
    if ( tableDataSource == nil )
        return 0;
    if( ( bucketCount = [tableDataSource count] ) < 1 || bucketCount <= section || (target_section = [tableDataSource objectAtIndex:section ]) == nil )
        return 0;
    return [ (NSMutableArray*)target_section count ];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:    [NSString stringWithFormat:@"Cell %i",indexPath.section]];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];
    }

    cell.textLabel.text = (NSString*)[ (NSMutableArray*)[tableDataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return @"City";
    }
    else if(section == 1)
    {
        return @"Person";
    }
    else 
    {
        return @"Nothing;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    if (indexPath.row == 0) 
    {
        LondonViewController *londonViewController = [[LondonViewController alloc] initWithNibName:@"LondonViewController" bundle:nil];
        taipeiViewController.title = @"London Info";
        [self.navigationController pushViewController:londonViewController animated:YES];   
        [londonViewController release];
    }

    else if (indexPath.row == 1) 
    {
        NewYorkViewController *newYorkViewController = [[NewYorkViewController alloc] initWithNibName:@"NewYorkViewController" bundle:nil];
        newYorkViewController.title = @"New York Info";
        [self.navigationController pushViewController:newYorkViewController animated:YES];   
        [newYorkViewController release];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

Ответы [ 2 ]

0 голосов
/ 06 октября 2011

ссылка на NSIndexpath: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/Reference/Reference.html

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    NSUInteger section = indexPath.section;
    NSUInteger row = indexPath.row;

    if (section == 0) 
    {
        LondonViewController *londonViewController = [[LondonViewController alloc] initWithNibName:@"LondonViewController" bundle:nil];
        taipeiViewController.title = @"London Info";
        [self.navigationController pushViewController:londonViewController animated:YES];   
        [londonViewController release];
    }

    else if (section == 1) 
    {
        NewYorkViewController *newYorkViewController = [[NewYorkViewController alloc] initWithNibName:@"NewYorkViewController" bundle:nil];
        newYorkViewController.title = @"New York Info";
        [self.navigationController pushViewController:newYorkViewController animated:YES];   
        [newYorkViewController release];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
0 голосов
/ 06 октября 2011

В

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Вы должны проверить значение indexPath.section, чтобы определить сечение выбранной ячейки.

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