1

詳細ビューにデータをロードしようとしています。誰でも見て、なぜ表示されないのかを知ることができますか? 詳細ビューではなく、ルートビューコントローラーに正常にロードされます。

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開発者に尋ねました. すべての情報源は、何か違うことを言っているようです。

4

1 に答える 1

1

最初の問題は、DetailViewController の setXXX メソッドがスーパー setXXX を呼び出そうとすることですが、DetailViewController は UIViewController のサブクラスであるため、UIViewController にはそのようなメソッドがないため、スーパーへの呼び出しは失敗します。setXXX メソッドの super への呼び出しを削除します。

2 番目の問題は、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];
}

上記の変更は機能するはずですが、(UI コントロール自体を使用してデータを保持するのではなく) DetailViewController に値を保持する ivar を作成することを検討することをお勧めします。次に、@property と @synthesize を使用してそれらのプロパティを作成します。プロパティは、DetailViewController が作成された直後に設定でき、ビューの viewDidLoad で、UI コントロールをプロパティ値に設定できます。これにより、DetailViewController は ui の更新方法をより詳細に制御できるようになり、呼び出し元に影響を与えずに ui を変更できるようになり、プロパティを設定するために表示する必要がなくなります。

于 2010-03-01T14:29:54.710 に答える