如何将表格视图单元格链接到另一个特定的表格视图?

时间:2015-08-23 22:19:23

标签: ios objective-c uistoryboard

我想要做的是在表格视图中有一个县列表。单击某个县时,另一个表视图将显示您可以选择的资源列表。我正在使用故事板和Objective-C。 Here是我的故事板。

我不想将选项嵌套到一个表视图中,因为我认为有太多选择无法有效嵌套。 这是我的县名单表视图的.h文件:     // SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
@end

县列表视图的我的.m文件:     // SecondViewController.m

#import "SecondViewController.h"
#import "DetailViewController.h"

@interface SecondViewController ()
@property (nonatomic, strong) NSArray *tableData;
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableData = @[@"Carter", @"Greene", @"Hancock", @"Hawkins", @"Johnson", @"Sullivan", @"Unicoi", @"Washington"];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.destinationViewController isKindOfClass:[DetailViewController class]]) 
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *name = self.tableData[indexPath.row];
        [(CountyViewController *)segue.destinationViewController setName:name];

    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return self.tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = self.tableData[indexPath.row];

    return cell;
}
@end

我希望以表格视图格式显示的County Resources .h文件:     // CountyViewController.h

#import <UIKit/UIKit.h>

@interface CountyViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end

.m文件:     // CountyViewController.m

#import "CountyViewController.h"
#import "CountyDetail.h"

@interface CountyViewController ()
@end

@implementation CountyViewController {
    NSArray *counties;
}

@synthesize tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data
    counties = [NSArray arrayWithObjects:@"Resource1", @"Resource2", @"Resource3", @"Resource4", @"Resource5", @"Resource6", @"Resource7", @"Resource8", @"Resource9", nil];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

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

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [counties objectAtIndex:indexPath.row];
    return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showCountyInfo"]) 
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        CountyDetail *destViewController = segue.destinationViewController;
        destViewController.countyName = [counties objectAtIndex:indexPath.row];
    }
}
@end

最后,点击了资源的详细信息: // CountyDetail.h

#import <UIKit/UIKit.h>

@interface CountyDetail : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *countyLabel;
@property (nonatomic, strong) NSString *countyName;

@end

和.m文件: // CountyDetail.m

#import "CountyDetail.h"

@interface CountyDetail ()

@end

@implementation CountyDetail

@synthesize countyLabel;
@synthesize countyName;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Set the Label text with the selected county
    countyLabel.text = countyName;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

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

@end

所以我的目标是让县选择转到另一个表视图中的另一个资源数组。我猜测我需要更多阵列,但我不知道格式或结构。我希望这是足够的信息,如果有人能解释他们的答案,那将是非常有帮助的。谢谢!

1 个答案:

答案 0 :(得分:0)

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

您可以按照此委托将消息发送到另一个tableView或其他人。