【问题标题】:How to enter a new view from a UITableViewCell? (when the table view gets its information from a .plist)如何从 UITableViewCell 输入新视图? (当表格视图从 .plist 获取其信息时)
【发布时间】:2013-03-19 17:01:00
【问题描述】:

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

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

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


@end

ViewController.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

感谢您的任何帮助!

【问题讨论】:

  • 到底是什么问题?
  • 问题是我想在按下 tableviewcell 时打开一个新视图
  • 当我从 å plist 文件中获取我的 tableview 信息时,我不知道该怎么做。
  • 你在使用故事板吗?
  • 我发布的解决方案对您有用吗?

标签: objective-c xcode view tableview


【解决方案1】:

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

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-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多