0

i have two view controllers, FirstViewController and DetailViewController, switch back and forth.

in didSelectRowAtIndexPath method ---> updateRowNumber:indexPath.row, i pass the selected row number to DetailViewController.

the first time, it worked, FirstViewController can pass the selected row to DetailedViewController, after switch back and forth, it still get the last selected row.

anyone knows what i missed?

#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
max
  • 645
  • 3
  • 11
  • 22

2 Answers2

1

First off, if "dvController" is a property change the "dvController" references in your "didSelectRowAtIndexPath" method to "self.dvController".

Secondly, in your "DetailViewController" object, when the "updateRowNumber" method fires... it doesn't look like you do anything else (i.e. updating the views or whatever) after that, aside from updating the internal row number.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • i have the "updateRowNumber" method to reset the rowNumber, set label1 text in viewDidLoad with "label1.text = [NSString stringWithFormat:@"row %i was clicked ", rowNumber];" does that so say, the viewDidLoad is just get called once? – max Nov 11 '12 at 08:58
  • 1
    Yes, in your case "`viewDidLoad`" in your DetailViewController gets called only once because in your parent "`didSelectRowAtIndexPath`" method, you only instantiate your DetailViewController once. – Michael Dautermann Nov 11 '12 at 09:00
  • i add the following code and it works - (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]; } but is there another way such as "dvController reload" can fix this? – max Nov 11 '12 at 09:11
  • @MichaelDautermann: Michael if OP moves same UI code to the updateRowNumber method that he had in viewDidLoad - why it started working in that method and not in viewDidLoad? (maybe because [super viewDidLoad] was called "late" in his code?) –  Feb 17 '14 at 07:52
1

i add the following code according to Michael Dautermann's advice and it works

- (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];

}
hyde
  • 60,639
  • 21
  • 115
  • 176
max
  • 645
  • 3
  • 11
  • 22
  • If this is same code (I mean UI part) as you had in viewDidLoad - why wasn't it working there? (ps. Maybe because in your viewDidLoad you called [super viewDidLoad]; "too late"?) –  Feb 17 '14 at 07:49