【发布时间】:2010-01-27 04:31:44
【问题描述】:
我使用 IB 并取消选中“触摸时显示选择”,但它仍然在选中的单元格上显示蓝色突出显示。这是苹果的错误还是我出错了。
【问题讨论】:
标签: iphone uitableview
我使用 IB 并取消选中“触摸时显示选择”,但它仍然在选中的单元格上显示蓝色突出显示。这是苹果的错误还是我出错了。
【问题讨论】:
标签: iphone uitableview
这可能是 IB 中的一个错误,正如您在文档中看到的那样,表格视图没有任何属性来显示触摸时的选择。它是 tableview 单元格的属性。所以复选框不应该出现在 IB 中。也许你可以向苹果提交一个错误,看看他们是怎么说的。
为了获得效果,你应该这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
}
希望这会有所帮助。
【讨论】:
我也觉得这很可能是一个错误。但是,根据以下观察,我偶然发现了一个完美的解决方法。
highlighted on touch down and selected on touch up。-setHighlighted:animated: 和 -setSelected:animated: 都根据其选择样式突出显示单元格,即打开其中一个,而关闭另一个则突出显示单元格。highlighted 已关闭(通常情况下,以下解决方案只需要根据您的具体情况进行适当的调整即可轻松找出)。鉴于上述情况,只需继承 UITableViewCell 并覆盖 setHighlighted:animated: 而不调用 super 的实现。因此,所有打开highlighted 的操作都将被禁止,并且突出显示只会在触摸时发生,而不是在触摸时发生,这正是关闭“触摸时显示选择”时所期望的。
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
// hack to get the effect of unchecking "Show Selection on Touch" option of UITableView in IB which has no effect
}
在修饰时,单元格是selected,但不是animated。如果你想要动画,我发现调用如下所示的委托方法可以动画选择。
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_itemsTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
return indexPath;
}
【讨论】: