【问题标题】:UITableView not selecting properlyUITableView 没有正确选择
【发布时间】:2012-02-11 20:04:38
【问题描述】:

我正在开发一个 iPhone 应用程序,其中有一个 UITableView,它通过 URL 填充了 XML 提要。

比如说填充了三个单元格。

如果我点击第一个单元格没有任何反应,但是如果我点击第二个或第三个单元格,它会将我带到与单元格一相关的第二个屏幕,并且其他单元格也会发生同样的情况 - 没有点击它,点击另一个,它会将我带到上一个所选屏幕的第二个屏幕。

我从来没有遇到过这种情况,我很困惑。

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

}

谢谢, 尼克

【问题讨论】:

  • 请发布您的 cellForRowAtIndexPath: 代码
  • 听起来您在tableView:didSelectRowAtIndexPath: 中的索引值差了一个,但如果没有看到一些相关代码,则无法确定。
  • 感谢收看,上面贴了一些代码。
  • 尝试将NSLog(@"%@ called on row:%d with atm:%@",NSStringFromSelector(_cmd),indexPath.row,atm.atmName); 放在您发布的两种方法的末尾附近。您使用的是哪个版本的 Xcode 和 iOS?
  • 我刚刚注意到我的问题 - 我使用的是“didDeselectRowAtIndexPath”,而不是“didSelectRowAtIndexPath”。傻我。

标签: objective-c uitableview


【解决方案1】:

您回答了自己的问题。问题是您使用了tableView:deselectRowAtIndexPath: 而不是tableView:didSelectRowAtIndexPath:

值得注意的是,这是因为 deselect 在字典中出现在 did 之前这一不幸的事实,因此,xcode 通常令人敬畏的代码完成提示您!

现在去恢复那些小时的调试!

【讨论】:

  • 我遇到了同样的问题。浪费了一个小时试图弄清楚发生了什么。需要多注意自动完成和语法,lol。
猜你喜欢
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多