在Objective-C中详细说明XCode TableViewController

时间:2015-08-09 02:53:43

标签: ios objective-c xcode tableviewcell

我有一个带有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

如果有人可以帮助我以最轻松的方式单独向每个表格视图单元格添加细节,我将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:0)

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

首先,我建议拖动原型&#34;表格查看单元格&#34;在您的表视图上。然后,您可以控制 -drag从该原型单元格到目标场景,以在单元格和下一个场景之间添加一个segue:

enter image description here

确保选择该原型单元并设置其故事板标识符(我使用&#34; Cell&#34;)。您需要引用该故事板标识符,如下面的代码示例所示。我还在IB中的单元格原型中配置了与外观相关的内容(如披露指示器),因此我不必担心在代码中执行此操作,我可以在IB中看到UI的外观。

现在您可以转到表格视图控制器并(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教程中都会更清楚地描述这个过程,其中包括对细胞原型的讨论&#34; (您的代码示例表明您使用的是一个早于单元原型的教程。)

答案 1 :(得分:0)

我认为代码与故事板之间的关系如下:

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