【问题标题】:Master Detail Application - Static Table in Detail View主从应用程序 - 详细视图中的静态表
【发布时间】:2012-09-16 17:31:45
【问题描述】:

我正在尝试使用故事板在 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

【问题讨论】:

    标签: iphone objective-c xcode


    【解决方案1】:

    我将尝试调试的第一件事是 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法。是返回值吗?试试把NSLog放到这个方法里检查一下。

    或许这会对你有所帮助:

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

    【讨论】:

    • 我不认为它被称为...?
    • 为什么?它是显示 UITableView 时调用的委托方法
    • 呃,好吧,也许是那时... :|我用那个方法拍了这个:“NSLog(@"%@", self.detailViewController);"没有任何输出。这是使用 NSLog 的正确方法吗?我想不...
    • 刚刚在 DetailViewController 中的 viewDidLoad 上完成了 NSLog 以查看 detailItem 设置为什么。它是空的。知道为什么会这样吗? ://
    • 哦,它正在被调用:L 我错误地将它放在 if 语句中,这显然是返回 false
    【解决方案2】:

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

    static NSString *CellIdentifier = @"VerbCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    

    【讨论】:

    • 很抱歉。我可能错过了您的问题-您已将表格移至详细信息视图?发布的代码在主视图中显示它。你想让桌子是静态的吗?发布的代码显示了动态内容的逻辑。你有固定数量的物品吗?该代码仍然实现 numberOfRowsInSection 和 numberOfSections - 这些实际上可能会使您的静态表无法正常工作。
    • 不,主视图包含一个列出动词的表。详细视图必须包含一个 6 行的静态表。 je、tu、il/elle、nous、vous 和 ils/elles。每个都有一些东西要显示在右边,一个正确的细节单元格。 :)
    • 哪一个没有出现(主动态或细节静态)?此外,您可能这样做了,但您是否确保为两者都设置了数据源?
    • Static on detail 没有显示出来,是的,两个表都设置了数据源和委托...
    • 你能把这个项目贴在我能看到的地方吗?我不确定你把这些标签网点连接到了什么。我设置静态表格内容的方式是通过调用 cell = [myTable cellForRowAtIndexPath:indexPath]; 来访问单元格。 (设置一个 indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 等等。然后我触摸 cell.textLabel.text = @"hello world";
    猜你喜欢
    • 2014-05-07
    • 1970-01-01
    • 2014-02-20
    • 2011-12-26
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    相关资源
    最近更新 更多