2

当我按下表格视图单元格时,我想出现一个新视图。但我似乎有一些问题,因为我的表格视图单元格从我制作的 .plist 文件中获取信息。这是我的代码。PS:我在 Storyboard 中制作这个。

视图控制器.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (copy, nonatomic) NSDictionary *firstTableView;
@property (copy, nonatomic) NSArray *firstTableViewKey;


@end

视图控制器.m:

#import "ViewController.h"


static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *tableView = (id)[self.view viewWithTag:1];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SectionsTableIdentifier];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"firstTableView" ofType:@"plist"];

    self.firstTableView = [NSDictionary dictionaryWithContentsOfFile:path];

    self.firstTableViewKey = [[self.firstTableView allKeys] sortedArrayUsingSelector:@selector(compare:)];

    tableView.backgroundColor = [UIColor clearColor];
    tableView.opaque = NO;
    tableView.backgroundView = nil;

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.firstTableViewKey count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = self.firstTableViewKey[section];
    NSArray *nameSection = self.firstTableView[key];
    return [nameSection count];

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.firstTableViewKey[section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    NSString *key = self.firstTableViewKey[indexPath.section];
    NSArray *nameSection = self.firstTableView[key];

    cell.textLabel.text = nameSection[indexPath.row];
    return cell;

}

@end

感谢您提供任何帮助!

4

1 回答 1

0

如果您想使用情节提要以模态方式推送视图,您可以使用:

UIStoryboard *storyboard       = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NewViewController *VC   = [storyboard instantiateViewControllerWithIdentifier:@"A_STRING_ID FOR_YOUR_VC_IN_STORYBOARDS"];
    [self presentViewController:VC animated:NO completion:nil];

可以从您的 plist 文件中填充字符串 (A_STRING_ID FOR_YOUR_VC_IN_STORYBOARDS) 的位置,或者您获得文本的位置

如果您想通过带有故事板的导航堆栈移动它

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
NewViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@"A_STRING_ID FOR_YOUR_VC_IN_STORYBOARDS"];
[self.navigationController pushViewController:VC animated:YES];
于 2013-03-20T14:20:53.783 回答