我有一个通过JSON动态创建的表视图,我希望它是这样的,当我点击表格视图中的其中一个项目时,我会转移到新的屏幕。 我使用我在网上找到的项目工作,但该项目适用于较旧的iOS并使用笔尖。 我想要传输的屏幕也必须是通过相同的JSON文件动态的。我需要知道如何创建动态屏幕以转移到如何使当前代码转移到该屏幕工作。
这是我当前要转移到新屏幕的代码(请记住它来自较旧的iOS):
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
PeopleDetailsViewController *detailViewController = [[PeopleDetailsViewController alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.details = self.data[indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
EDIT。这是我当前创建详细信息视图的代码。(需要动态的视图):
#import "PeopleDetailsViewController.h"
@interface PeopleDetailsViewController ()
@end
@implementation PeopleDetailsViewController
@synthesize details;
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (NSString *)name {
return [NSString stringWithFormat:@"%@ %@", self.details[@"Tag"], self.details[@"B"]];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = [self name];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
switch (indexPath.row) {
case 0:
cell.textLabel.text = [self name];
cell.detailTextLabel.text = @":Tag";
break;
case 1: {
cell.detailTextLabel.text = @":Value";
cell.textLabel.text = self.details[@"Value"];
break;
default:
break;
}
}
return cell;
}
答案 0 :(得分:0)
你(大部分)你需要的一切。您的表视图数据源现在是硬编码的,因此您必须更改代码的这一部分。方法名称非常明显。
你应该做什么:
在PeopleDetailsViewController
中添加一个方法,例如' -(void) setDetails : (type)name
,用于将详细信息传递给新控制器。在此函数结束时,您应该调用[self.tableView reloadData];
- 这将告诉您的表视图数据已更改并应重新加载(调用#pragma mark - Table view data source
下的方法)
更改上述mentiod方法,以便它们不会返回硬编码值,而是传递详细信息。
如果您需要非标准表格单元格,可以在Interface Bulder中设计它们,并使用IBOutlets
为它们分配一个自定义类,然后用类似的数据填充它们。