0

我正在尝试使用故事板在 Xcode 4 中编写应用程序。这是一个主详细信息应用程序,使用表格和详细信息视图一切正常。但在我的详细视图中,我希望有一个静态表来显示数据。以分组表格样式的方式,左侧是“键”,右侧是“值”,如果这是一种放置方式......所以,在我将表格放入我的 UIView 之前,一切正常。显然你必须把它放在 UITableView 中才能工作,所以我删除了 Xcode 为我制作的 UIView 并放在 UITableView 的位置。我将其与标识符、标题等设置完全相同(我认为),然后将表格单元格与插座连接起来,等等。但是现在当我进入视图时,我只是得到一个空表(嗯,不是空的,只是所有的行都说“详细信息” 而不是我想要的实际数据)。我不明白为什么!D:我什至把 DetailViewController.h 也改成了“UITableViewController”!无济于事... :( 有人可以告诉我我做错了什么!我敢打赌这真的很简单... :L 这是我的代码

主视图控制器.h

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController

@property (strong, nonatomic) DetailViewController *detailViewController;
@property (strong) NSMutableArray *verbs;

@end

主视图控制器.m

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "VerbData.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

@synthesize verbs = _verbs;

- (void)awakeFromNib
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    }
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.detailViewController = (DetailViewController *)  [[self.splitViewController.viewControllers lastObject] topViewController];
    self.title = @"Verbs";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _verbs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VerbCell"];

    VerbData *verb = [self.verbs objectAtIndex:indexPath.row];
    cell.textLabel.text = verb.infinitive;
    cell.detailTextLabel.text = verb.english;
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        VerbData *object = [self.verbs objectAtIndex:indexPath.row];
        self.detailViewController.detailItem = object;
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        VerbData *object = [self.verbs objectAtIndex:indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }
}

@end

DetailViewController.h

#import <UIKit/UIKit.h>
#import "VerbData.h"

@interface DetailViewController : UITableViewController <UISplitViewControllerDelegate>

@property (strong, nonatomic) VerbData *detailItem;

@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *jeOutlet;
@property (weak, nonatomic) IBOutlet UILabel *tuOutlet;
@property (weak, nonatomic) IBOutlet UILabel *ilOutlet;
@property (weak, nonatomic) IBOutlet UILabel *nousOutlet;
@property (weak, nonatomic) IBOutlet UILabel *vousOutlet;
@property (weak, nonatomic) IBOutlet UILabel *ilsOutlet;

@end

细节视图控制器.m

#import "DetailViewController.h"

@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation DetailViewController

#pragma mark - Managing the detail item
@synthesize detailItem = _detailItem;
@synthesize jeOutlet = _jeOutlet;
@synthesize tuOutlet = _tuOutlet;
@synthesize ilOutlet = _ilOutlet;
@synthesize nousOutlet = _nousOutlet;
@synthesize vousOutlet = _vousOutlet;
@synthesize ilsOutlet = _ilsOutlet;

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.detailDescriptionLabel.text = self.detailItem.english;
        self.jeOutlet.text = self.detailItem.je;
        self.tuOutlet.text = self.detailItem.tu;
        self.ilOutlet.text = self.detailItem.il;
        self.nousOutlet.text = self.detailItem.nous;
        self.vousOutlet.text = self.detailItem.vous;
        self.ilsOutlet.text = self.detailItem.ils;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = self.detailItem.infinitive;
    [self configureView];
}

- (void)viewDidUnload
{
    [self setJeOutlet:nil];
    [self setTuOutlet:nil];
    [self setIlOutlet:nil];
    [self setNousOutlet:nil];
    [self setVousOutlet:nil];
    [self setIlsOutlet:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

@end
4

2 回答 2

0

我将尝试调试的第一件事是-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法。是返回值吗?尝试将NSLog放入此方法并检查它。

或者也许这会帮助你:

注意:如果要更改单元格的背景颜色(通过 UIView 声明的 backgroundColor 属性设置单元格的背景颜色),您必须在委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行,而不是在 tableView :cellForRowAtIndexPath: 数据源的。更改组样式表视图中单元格的背景颜色在 iOS 3.0 中的效果与以前版本的操作系统不同。它现在影响圆角矩形内部的区域,而不是它外部的区域。

于 2012-09-16T18:35:47.280 回答
0

代码必须处理 dequeueReusableCellWithIdentifier 回答 nil 的情况...

static NSString *CellIdentifier = @"VerbCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
于 2012-09-16T18:53:41.493 回答