在表视图中的一个选项中选择特定行

发布于 2024-12-08 19:51:10 字数 4296 浏览 0 评论 0原文

我创建了一个包含两个部分的表。每个部分都有四个单元格。当用户按下某个单元格时,我想通过导航控制器推送到新视图。

然而有两个部分。我不知道如何区分所选单元格属于“

- (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

I create a table with two sections. Each section has four cells.I want to push to a new view by navigation controller when a certain cell is pressed by user.

However there are two sections. I don't know how to distinguish the cell selected belongs to which section in

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

Please give me some tips. Thank you very much.

Here's my 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2024-12-15 19:51:10

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

应该检查 indexPath.section 值以确定所选单元格的部分。

In

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

you should check indexPath.section value to determine the section of the selected cell.

飞烟轻若梦 2024-12-15 19:51:10

参考 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];
}

refer a NSIndexpath reference: 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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文