我如何 link 一个 table 视图单元格到另一个特定的 table 视图?
How do I link a table view cell to another specific table view?
我想做的是在 table 视图中有一个县列表。当您单击一个县时,另一个 table 视图将显示您可以 select 的资源列表。我正在使用故事板和 Objective-C。
Here 是我的故事板。
我不想将选项嵌套到一个 table 视图中,因为我认为有太多选择无法有效嵌套。
这是我的县列表 table 视图的 .h 文件:
// SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
@end
我的县列表 .m 文件 table 查看:
// 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
我想以table视图格式显示的县资源.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
所以我的目标是让县 selection 转到另一个 table 视图中的另一个资源数组。我猜我需要更多的数组,但我只是不知道格式或结构。我希望这是足够的信息,如果有人能解释他们的答案,那将非常有帮助。谢谢!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
你可以跟随这个delegate发送消息给另一个tableView或其他人。
我想做的是在 table 视图中有一个县列表。当您单击一个县时,另一个 table 视图将显示您可以 select 的资源列表。我正在使用故事板和 Objective-C。 Here 是我的故事板。
我不想将选项嵌套到一个 table 视图中,因为我认为有太多选择无法有效嵌套。 这是我的县列表 table 视图的 .h 文件: // SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
@end
我的县列表 .m 文件 table 查看: // 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
我想以table视图格式显示的县资源.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
所以我的目标是让县 selection 转到另一个 table 视图中的另一个资源数组。我猜我需要更多的数组,但我只是不知道格式或结构。我希望这是足够的信息,如果有人能解释他们的答案,那将非常有帮助。谢谢!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
你可以跟随这个delegate发送消息给另一个tableView或其他人。