【发布时间】: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”。傻我。