数据未在详细视图中加载

发布于 2024-08-23 08:38:25 字数 4290 浏览 6 评论 0原文

我正在尝试将数据加载到详细视图中。任何人都可以看一下并了解为什么它不显示吗?它在我的 rootviewcontroller 中加载得很好,只是在详细视图中加载得不好。

DetailViewController.m

 #import "DetailViewController.h"


@implementation DetailViewController




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}



- (void)setIcon:(UIImage *)newIcon
{
    [super setIcon:newIcon];
    iconView.image = newIcon;
}

- (void)setPublisher:(NSString *)newPublisher
{
    [super setPublisher:newPublisher];
    publisherLabel.text = newPublisher;
}


- (void)setName:(NSString *)newName
{
    [super setName:newName];
    nameLabel.text = newName;
}



- (void)dealloc
{
    [iconView release];
    [publisherLabel release];
    [nameLabel release];
    [priceLabel release];

    [super dealloc];
}


@end

detailviewcontroller.h

 #import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>




@interface DetailViewController : UIViewController {
    IBOutlet UIImageView *iconView;
    IBOutlet UILabel *publisherLabel;
    IBOutlet UILabel *nameLabel;
    IBOutlet UILabel *priceLabel;

}




@end

RootViewControllerPoints.m

#import "RootViewControllerPoints.h"
#import "DetailViewController.h"




#define USE_INDIVIDUAL_SUBVIEWS_CELL    1

#define DARK_BACKGROUND  [UIColor colorWithRed:151.0/255.0 green:152.0/255.0 blue:155.0/255.0 alpha:1.0]
#define LIGHT_BACKGROUND [UIColor colorWithRed:172.0/255.0 green:173.0/255.0 blue:175.0/255.0 alpha:1.0]


@implementation RootViewController

@synthesize tmpCell, data;


#pragma mark View controller methods

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the table view.
    self.tableView.rowHeight = 73.0;
    self.tableView.backgroundColor = DARK_BACKGROUND;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // Load the data.
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    self.data = [NSArray arrayWithContentsOfFile:dataPath];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            return YES;
        default:
            return NO;
    }
}



#pragma mark Table view methods

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ApplicationCell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

#if USE_INDIVIDUAL_SUBVIEWS_CELL
        [[NSBundle mainBundle] loadNibNamed:@"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
        cell = tmpCell;
        self.tmpCell = nil;

#endif
    }

    // Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
    cell.useDarkBackground = (indexPath.row % 2 == 0);

    // Configure the data for the cell.

    NSDictionary *dataItem = [data objectAtIndex:indexPath.row];
    cell.icon = [UIImage imageNamed:[dataItem objectForKey:@"Icon"]];
    cell.publisher = [dataItem objectForKey:@"Publisher"];
    cell.name = [dataItem objectForKey:@"Name"];


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    detailViewController. = [data objectAtIndex:indexPath.row];
   [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}



@end

这已经困扰我很长一段时间了,我查看了许多示例、教程,甚至询问了其他 iphone 开发人员。所有的来源似乎都在说不同的东西。

I'm trying to get my data to load in my detail view. Can any one take a look and see why it isnt showing? It loads fine in my rootviewcontroller, just not the detail view.

DetailViewController.m

 #import "DetailViewController.h"


@implementation DetailViewController




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}



- (void)setIcon:(UIImage *)newIcon
{
    [super setIcon:newIcon];
    iconView.image = newIcon;
}

- (void)setPublisher:(NSString *)newPublisher
{
    [super setPublisher:newPublisher];
    publisherLabel.text = newPublisher;
}


- (void)setName:(NSString *)newName
{
    [super setName:newName];
    nameLabel.text = newName;
}



- (void)dealloc
{
    [iconView release];
    [publisherLabel release];
    [nameLabel release];
    [priceLabel release];

    [super dealloc];
}


@end

detailviewcontroller.h

 #import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>




@interface DetailViewController : UIViewController {
    IBOutlet UIImageView *iconView;
    IBOutlet UILabel *publisherLabel;
    IBOutlet UILabel *nameLabel;
    IBOutlet UILabel *priceLabel;

}




@end

RootViewControllerPoints.m

#import "RootViewControllerPoints.h"
#import "DetailViewController.h"




#define USE_INDIVIDUAL_SUBVIEWS_CELL    1

#define DARK_BACKGROUND  [UIColor colorWithRed:151.0/255.0 green:152.0/255.0 blue:155.0/255.0 alpha:1.0]
#define LIGHT_BACKGROUND [UIColor colorWithRed:172.0/255.0 green:173.0/255.0 blue:175.0/255.0 alpha:1.0]


@implementation RootViewController

@synthesize tmpCell, data;


#pragma mark View controller methods

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the table view.
    self.tableView.rowHeight = 73.0;
    self.tableView.backgroundColor = DARK_BACKGROUND;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // Load the data.
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    self.data = [NSArray arrayWithContentsOfFile:dataPath];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            return YES;
        default:
            return NO;
    }
}



#pragma mark Table view methods

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ApplicationCell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

#if USE_INDIVIDUAL_SUBVIEWS_CELL
        [[NSBundle mainBundle] loadNibNamed:@"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
        cell = tmpCell;
        self.tmpCell = nil;

#endif
    }

    // Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
    cell.useDarkBackground = (indexPath.row % 2 == 0);

    // Configure the data for the cell.

    NSDictionary *dataItem = [data objectAtIndex:indexPath.row];
    cell.icon = [UIImage imageNamed:[dataItem objectForKey:@"Icon"]];
    cell.publisher = [dataItem objectForKey:@"Publisher"];
    cell.name = [dataItem objectForKey:@"Name"];


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    detailViewController. = [data objectAtIndex:indexPath.row];
   [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}



@end

This has been bugging me for quite a while, I've looked at numerous examples, tutorials and even asked other iphone devs. Everything source seems to say something different.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2024-08-30 08:38:25

第一个问题是 DetailViewController 中的 setXXX 方法尝试调用 super setXXX,但由于 DetailViewController 是 UIViewController 的子类,因此对 super 的调用将失败,因为 UIViewController 没有此类方法。删除 setXXX 方法中对 super 的调用。

第二个问题是 setXXX 方法直接在 DetailViewController 上设置控件,但在加载视图之前控件将无法访问,因此如果在调用 pushViewController 之前调用这些方法,它将不起作用。

如果您按如下方式更改 didSelectRowAtIndexPath 中的代码,它应该可以工作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
   [self.navigationController pushViewController:detailViewController animated:YES];
   [detailViewController setName:@"name here"];
   [detailViewController setPublisher:@"publisher here"];
   [detailViewController setIcon:yourImageVariableHere];       
   [detailViewController release];
}

虽然上述更改应该可以工作,但您可能需要考虑创建 ivars 来保存 DetailViewController 中的值(而不是使用 ui 控件本身来保存数据)。然后使用@property和@synthesize为它们创建属性。可以在创建 DetailViewController 后立即设置属性,并且在视图的 viewDidLoad 中,可以将 ui 控件设置为属性值。这将使 DetailViewController 能够更好地控制其 ui 的更新方式,允许您在不影响调用者的情况下更改 ui,并且不需要显示它来设置其属性。

First problem is that the setXXX methods in DetailViewController try to make calls to super setXXX but since DetailViewController is a subclass of UIViewController those calls to super will fail because UIViewController doesn't have such methods. Remove the calls to super in the setXXX methods.

Second problem is that the setXXX methods are setting the controls on the DetailViewController directly but the controls won't be accessible until the view is loaded so it won't work if the methods are called before the pushViewController call.

If you change the code in didSelectRowAtIndexPath as follows it should work:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
   [self.navigationController pushViewController:detailViewController animated:YES];
   [detailViewController setName:@"name here"];
   [detailViewController setPublisher:@"publisher here"];
   [detailViewController setIcon:yourImageVariableHere];       
   [detailViewController release];
}

Although the above change should work, you might want to consider creating ivars to hold the values in DetailViewController (instead of using the ui controls themselves to hold the data). Then create properties for them using @property and @synthesize. The properties can be set immediately after DetailViewController is created and in the viewDidLoad of the view, you can set the ui controls to the property values. This will give the DetailViewController more control over how its ui is updated, allow you to change the ui without affecting callers, and it doesn't need to be displayed to set its properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文