Rutvij Kotecha
Rutvij Kotecha

Reputation: 919

Delayed response in TableView

I am trying to make a simple program which involves a tableView. When I click on a cell in the tableView, it should reveal more information about the cell that was clicked on DetailedViewController. However, when I click the cell, it remains highlighted and it does not reveal the information of the cell. So I used NSLog() statements to identify where the program was getting stuck. NSLog() statement that I introduced in didDeselectRowAtIndexPath method indicates that I get a response for the cell that was click prior to the last one.

Any idea why is this happening? Also, why do I not see the DetailedViewController when I click the cell?

For the sake of brevity, Numbers.h contains two variable: NSString * name and int long long number. DetailedViewController is a simple VC with two labels: nameField and numberField and they are suppose to get updated when I click the respective cell.

I would sincerely appreciate any help. Thanks.

#import "SecondViewController.h"
#import "DetailedViewController.h"
#import "Numbers.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize numberList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog(@"loaded Nib file");
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"View Numbers";
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"Loaded view controller");
    Numbers *num1 = [[Numbers alloc] init];
    Numbers *num2 = [[Numbers alloc] init];
    num1.name = @"Number 1";
    num1.number = 12345;

    num2.name = @"Number 2";
    num2.number = 23456;

    numberList = [[NSMutableArray alloc]  initWithObjects:num1,num2, nil];

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload {
    [self setNumberList:nil];
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return numberList.count;
}

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

- (UITableViewCell *)tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    NSLog(@"In cellForRowAtIndexPath:");
    static NSString *cellIndetifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2    reuseIdentifier:cellIndetifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }

    cell.textLabel.text = [[numberList objectAtIndex:indexPath.row] name];

    return cell;

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row selected = %d", indexPath.row);

    Numbers *numberToDisplay = [[Numbers alloc] init];
    numberToDisplay.name = [[numberList objectAtIndex:indexPath.row] name]; // why can I not use dot notation here?
    numberToDisplay.number = [[numberList objectAtIndex:indexPath.row] number]; // why can I not use dot notation here?

    DetailedViewController *detailedViewController = [[DetailedViewController alloc] initWithNibName:@"DetailedViewController" bundle:nil];
    detailedViewController.nameLabel.text = numberToDisplay.name;
    detailedViewController.numberLabel.text = [NSString stringWithFormat:@"%lli", numberToDisplay.number];

    [self.navigationController pushViewController:detailedViewController animated:YES];
}

@end

Upvotes: 3

Views: 303

Answers (2)

Simon Germain
Simon Germain

Reputation: 6844

Change didDeselectRowAtIndexPath for didSelectRowAtIndexPath. That should give you information about the cell that's currently being selected.

The cell you're getting from didDeselectRowAtIndexPath is the row that gets deselected, after you selected a new cell. That's why it's behind.

Upvotes: 6

ikuramedia
ikuramedia

Reputation: 6058

You've implemented didDeselect instead of didSelect - just change the method name and try again.

Upvotes: 2

Related Questions