【发布时间】:2011-06-17 03:28:32
【问题描述】:
在我正在开发的 iOS 应用程序(iOS SDK 4.2)中实现 UITableView 时遇到了问题。如果我在表格视图中点击一个单元格,然后滚动视图以使该单元格离开屏幕,当它返回到视图中时,最近选择的单元格已被重新选择。
为了测试这一点,我新建了一个基于视图的应用程序项目,在Interface Builder中拖出一个UITableView,并将视图控制器设置为表格视图的数据源和委托,并将以下代码放入视图控制器中:
- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section {
return 12;
}
- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *cellID = @"aCellID";
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!aCell) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
aCell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row +1];
return aCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath];
[aCell setSelected:NO animated:YES];
}
当我运行这个(在模拟器或设备上)时,如果我要点击任何单元格(例如单元格 12),它将被选中和取消选中。在此之后,当前未选择表中的单元格,如果我滚动视图以使最近选择的单元格(在本例中为 12 个)离开屏幕,当它重新出现时将再次被选中。
这是UITableView 的预期默认行为吗?或者可能是我的实施中的错误?在这种情况下取消选择此单元格的最佳方法是什么?
【问题讨论】:
标签: cocoa-touch ios uitableview uiscrollview