0

FirstViewController と DetailViewController の 2 つのビュー コントローラーがあり、前後に切り替えます。

didSelectRowAtIndexPath メソッド ---> updateRowNumber:indexPath.row で、選択した行番号を DetailViewController に渡します。

初めて機能したとき、FirstViewController は選択された行を DetailedViewController に渡すことができます。切り替えた後でも、最後に選択された行が取得されます。

誰かが私が逃したものを知っていますか?

#import "FirstViewController.h"
#import "AppDelegate.h"
#import "DetailViewController.h"
#import "PartyListCustomViewCell.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize dvController;  // it is a DetailViewController

- (void) viewDidLoad
{
contentArray = [[NSArray arrayWithObjects:@"heyun", @"wushi", @"ios", nil]retain];

[super viewDidLoad];

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) dealloc
{
    [dvController release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
}

- (void)viewDisDisappear:(BOOL)animated
{
    [super viewDidDisappear: animated];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [contentArray count];
}

// custom row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // dynamic with whether the cell has the party image or not..
    return 160;
}

// Customize the appearance of table view cells
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:        (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PartyListCustomCell";

    PartyListCustomViewCell *plcc = [tableView     dequeueReusableCellWithIdentifier:CellIdentifier];

    if(plcc == nil)
    {
        plcc = [[[PartyListCustomViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    // set up custom cell
    plcc.partyTitle.text = @"partyTitle";


    return plcc;

}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (dvController == nil)
    {
        DetailViewController *aController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

        self.dvController = aController;

        [aController release];
    }   

    [dvController updateRowNumber:indexPath.row];
    [[self navigationController] pushViewController:dvController animated:YES];

}

@end



#import "DetailViewController.h"


@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize rowNumber;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
    }
    return self;
}

- (void) updateRowNumber: (int) theindex
{

    rowNumber = theindex + 1;    
}

- (void)dealloc
{

    [super dealloc];
}

- (void)viewDidLoad
{

    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];

    label1.text = [NSString stringWithFormat:@"row %i was clicked ", rowNumber];

    [self.view addSubview: label1];

    [super viewDidLoad];
    [label1 release];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{   
    [super viewWillAppear: animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

2 に答える 2

1

まず、 " dvController"がプロパティの場合、" "メソッドの" dvController"参照を""に変更します。didSelectRowAtIndexPathself.dvController

次に、 " DetailViewController"オブジェクトで、 " updateRowNumber"メソッドが起動すると、内部の行番号を更新する以外は、その後は何も実行していないように見えます(つまり、ビューの更新など)。

于 2012-11-11T08:29:53.233 に答える
1

Michael Dautermann のアドバイスに従って次のコードを追加すると、機能します

- (void) updateRowNumber: (int) theindex
{

    rowNumber = theindex + 1;

    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];

    label1.text = [NSString stringWithFormat:@"row %i was clicked ", rowNumber];
    [self.view addSubview: label1];
    [label1 release];

}
于 2012-11-11T09:20:45.133 に答える