Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView not selecting properly

I am working on an iPhone app, where I have a UITableView which is being populated with an XML feed through a URL.

Say for instance three of the cells are populated.

If I tap on the first cell nothing happens, however if I tap on the second or third cell, it takes me to the second screen related to cell one, and the same happens with the other cells - tap on it nothing, tap on another and it takes me to the second screen of the previous one selected.

I have never had this happen before and am rather confused.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                              reuseIdentifier:@"UITableViewCell"];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    LocationsItem *atm = [[locations atms] objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[atm atmName]];

    float distance = [[atm atmDistance] floatValue];
    NSString *distanceString = [NSString stringWithFormat:@"%0.2f miles from current location", distance];
    [[cell detailTextLabel] setText:distanceString];

    return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    LocationsItem *atm = [[locations atms] objectAtIndex:[indexPath row]];

    ATMDetailsController *detailsController = [[ATMDetailsController alloc] init];

    [detailsController setCurrentATM: atm];

    [[self navigationController] pushViewController:detailsController animated:YES];

}

Thanks, Nick

like image 289
Nick Avatar asked Jan 18 '23 12:01

Nick


1 Answers

You answered your own question. The issue is that you used tableView:deselectRowAtIndexPath: instead of tableView:didSelectRowAtIndexPath:

What is noteworthy is that this is from the unfortunate fact that deselect comes before did in the dictionary, and therefore, xcode's normally awesome code completion hinks you!

Now to go get those hours of debugging back!

like image 173
Troy Avatar answered Jan 30 '23 08:01

Troy