2012-02-11 65 views
4

我正在開發一個iPhone應用程序,其中有一個UITableView,它正在通過URL填充XML feed。UITableView沒有正確選擇

舉例說,三個單元格被填充。

如果我點擊第一個單元格沒有任何反應,但是如果我點擊第二個或第三個單元格,它將把我帶到與第一個單元格相關的第二個屏幕,並且與其他單元格相同 - ,點擊另一個,它會將我帶到上一個選中的第二個屏幕。

我從來沒有發生過這種情況,而且相當困惑。

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

} 

感謝, 尼克

+3

任何示例代碼? – TigerCoding 2012-02-11 20:08:08

+1

請發佈您的cellForRowAtIndexPath:代碼 – Rayfleck 2012-02-11 20:10:29

+1

聽起來像您在'tableView:didSelectRowAtIndexPath:'中的索引是關閉的,但沒有看到一些相關的代碼,這是不可能的。 – 2012-02-11 20:11:34

回答

9

你回答了你自己的問題。問題是,你用tableView:deselectRowAtIndexPath:代替tableView:didSelectRowAtIndexPath:

值得注意的是,這是不幸的事實deselect進來字典did之前,因此,Xcode的正常真棒代碼完成hinks你!

現在去調試回來的小時!

+0

我也有同樣的問題。浪費了一個小時,試圖弄清楚發生了什麼。需要更多地關注自動完成和語法,哈哈。 – Tander 2014-01-07 07:31:01