繁体   English   中英

XCode TableViewController详细介绍在Objective-C中

[英]XCode TableViewController to detail in Objective-C

我有一个带有4个标签的TabBarController,其中3个是表格视图。 我试图为每个表格视图单元格放置一个细节,但我认为故事板效率不高,因为我有50多个细节页面。 我对所有这些都是新手,并且尝试了几个小时来找出如何将详细信息链接到每个选项卡。 我的表视图从Second View Controller开始。 这是SecondViewController.m:

#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

如果有人能够以最轻松的方式帮助我分别向每个表视图单元添加详细信息,我将不胜感激。 谢谢!

如果使用情节提要,则过程非常简单。

首先,我建议将原型“表格视图单元格”拖到您的表格视图上。 然后,您可以控制 -从该原型单元拖动到目标场景,以在该单元和下一场景之间添加序列:

在此处输入图片说明

确保选择该原型单元并设置其情节提要标识符(我使用“ Cell”)。 您将需要引用该情节提要标识符,如下面的代码示例所示。 我还在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. 代码实现了应用程序的功能。
  2. 故事板包含许多场景,这些场景实现了用户界面,包括数据演示,数据输入,数据输出。
  3. 代码从这些场景读取数据,并将结果输出到场景。
  4. 代码是内部逻辑功能实体,情节提要是用户界面演示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM