То, что я пытаюсь сделать, это иметь список округов в виде таблицы. Когда вы нажимаете графство, в другом представлении таблицы отображается список ресурсов, которые вы можете выбрать. Я использую раскадровки и 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
графства Ресурсного .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
Таким образом, моя цель состоит в том, чтобы выбор графства переходил к другому массиву ресурсов в другом виде таблицы. Я предполагаю, что мне понадобится гораздо больше массивов, но я просто не знаю формат или структуру. Надеюсь, этого достаточно, и если кто-нибудь сможет объяснить их ответ, это будет очень полезно. Благодаря!
Вы спрашиваете, как организовать данные? Если это так, вы можете создать NSDictionary в своем втором контроллере представления с «ключом» как графством и «значением» в качестве массива ресурсов, прикрепленных к этому округу. – realtimez
@realtimez Я просто хочу знать, как использовать объектив c, чтобы создать другой вид таблицы, но данные для ячеек будут меняться в зависимости от того, какая ячейка вы нажали. –