2015-08-09 43 views
1

我有一个TabBarController有4个选项卡,其中3个是表视图。我试图为每个表格视图单元格添加一个细节,并且我不认为故事板是有效的,因为我有超过50个详细页面。我对这一切都很陌生,我试图找出如何将细节链接到每个标签几个小时。我的表格视图从第二个视图控制器开始。 这里是SecondViewController.m:XCode TableViewController详细介绍Objective-C

#import "SecondViewController.h" 
@implementation SecondViewController 
{ 
    NSArray *tableData; 
} 
@synthesize tableData; 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    tableData = [NSArray arrayWithObjects:@"Carter", @"Greene", @"Hancock", @"Hawkins", @"Johnson", @"Sullivan", @"Unicoi", @"Washington", nil]; 
    [super viewDidLoad]; 

} 

#pragma mark - TableView Data Source methods 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{ 
    return [tableData count]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"]; 
    } 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 


    return cell; 
} 

@end 

这里是SecondViewController.h:

#import <UIKit/UIKit.h> 

@interface SecondViewController : UIViewController <UITableViewDelegate, 
UITableViewDataSource> 

@property(nonatomic, retain) NSArray *tableData; 
@end 

如果这会有所帮助,这里是我的故事板。

imgur.com/f9xdo0E

如果有人能帮助我单独以最无痛的方式详细信息添加到每个表视图单元格,我将不胜感激。谢谢!

+0

您可能需要解释更多关于您的细节是什么意思?当用户点击一个单元格时打开一个详细视图控制器?请详细说明。 –

+0

我使用Swift,所以不幸的是我可能无法在Obj-C中为您详细说明,但我可以提供高级解决方案。你应该创建另一个'UIViewController' .....说'DetailViewController'。您可以设置它的视觉方式,然后当用户点击一个“TableViewCell”时,对DetailViewController执行一个segue并将数据从单元传递到视图控制器。 –

+1

你为什么认为故事板效率不高?根据我的经验,在故事板中可以有一些非常愚蠢的对象,并且它们在运行时被拆分并加载到零件中,很少有任何运行时问题。另外,你是否真的有50 *个不同的*详细页面,或者它们都是相同的布局,只是不同的内容? – zpasternack

回答

0

如果使用故事板,过程相当简单。

首先,我建议将一个原型“table view cell”拖到你的表格视图上。然后,您可以控制从原型细胞到目的地的场景 -drag增加该小区和下一场景之间的赛格瑞:

enter image description here

确保选择原型细胞,并设置其故事板标识符(我用“细胞”)。您需要参考故事板标识符,如下面的代码示例所示。我还在IB中的单元原型中配置了外观相关的东西(如披露指标),所以我不必担心在代码中这样做,我可以看到UI在IB中的样子。 (a)简化cellForRowAtIndexPath(因为在使用单元格原型时,您不需要关于if (cell == nil) ...的逻辑)。同时也实现prepareForSegue将数据传递到目的地的场景:

// SecondViewController.m 

#import "SecondViewController.h" 
#import "DetailsViewController.h" 

@interface SecondViewController() 
@property (nonatomic, strong) NSArray *tableData; 
@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:[DetailsViewController class]]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     NSString *name = self.tableData[indexPath.row]; 
     [(DetailsViewController *)segue.destinationViewController setName:name]; 
    } 
} 

- (IBAction)unwindToTableView:(UIStoryboardSegue *)segue { 
    // this is intentionally blank; but needed if we want to unwind back here 
} 

#pragma mark - Table view data source 

- (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 

很显然,这是假定您创建了一个DetailsViewController和指定为目的地场景的基类,然后创建一个你想要的任何值的属性通过此目标的场景:

// DetailsViewController.h 

#import <UIKit/UIKit.h> 

@interface DetailsViewController : UIViewController 

@property (nonatomic, copy) NSString *name; 

@end 

然后这个目标的场景将采取通过name值,并填写UILabel

// DetailsViewController.m 

#import "DetailsViewController.h" 

@interface DetailsViewController() 

@property (weak, nonatomic) IBOutlet UILabel *nameLabel; 

@end 

@implementation DetailsViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.nameLabel.text = self.name; 
} 

@end 

坦率地说,这个过程无疑会在任何UITableView教程中更加清晰地描述,其中包括对“细胞原型”的讨论(您的代码示例建议您使用早于细胞原型的较旧教程)。

+1

是的,非常感谢你!我开始理解代码和故事板之间的关系,这非常有帮助! –

0

我觉得代码和故事板之间的关系如下:

  1. 代码实现应用程序的功能。
  2. 故事板包含许多场景,这些场景实现了用户界面,包括数据显示,数据输入,数据输出。
  3. 代码从这些场景读取数据并将结果输出到场景。
  4. 代码是内部逻辑功能实体,故事板是用户界面演示文稿。