主细节应用 - 详细视图中的静态表

时间:2012-09-16 17:31:46

标签: iphone objective-c xcode

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

MasterViewController.h

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController

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

@end

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

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

2 个答案:

答案 0 :(得分:0)

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

或许这会对你有所帮助:

  

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

答案 1 :(得分:0)

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

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

if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}