我正在尝试将数据加载到详细视图中。谁能看一看,为什么不显示呢?它可以很好地加载到我的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开发人员。一切消息似乎都说了些不同。

最佳答案

第一个问题是,DetailViewController中的setXXX方法尝试调用超级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而不影响调用者,并且无需显示它即可设置其属性。

关于ios - 数据未在详细 View 中加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2348813/

10-13 07:55