【发布时间】:2011-09-04 00:05:03
【问题描述】:
在我的UITableView 中,当它进入编辑模式时,我希望只有少数几个单元格可供选择。我知道UITableView 类具有allowsSelectionDuringEditing 属性,但这适用于整个UITableView。我没有看到任何相关的委托方法可以在每个单元格的基础上进行设置。
我能想到的最佳解决方案是将allowsSelectionDuringEditing 设置为YES。然后,在didSelectRowAtIndexPath 中,如果表格视图正在编辑,则过滤掉任何不需要的选择。此外,在cellForRowAtIndexPath 中,将这些单元格selectionStyle 更改为无。
问题在于进入编辑模式不会重新加载UITableViewCells,因此他们的selectionStyle 在滚动到屏幕外之前不会改变。因此,在 setEditing 中,我还必须遍历可见单元格并设置它们的 selectionStyle。
这可行,但我只是想知道是否有更好/更优雅的解决方案来解决这个问题。附上我的代码的基本大纲。任何建议都非常感谢!谢谢。
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
if (self.editing && ![self _isUtilityRow:indexPath]) return;
// Otherwise, do the normal thing...
}
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// UITableViewCell* cell = ...
if (self.editing && ![self _isUtilityRow:indexPath])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
return cell;
}
- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing)
{
for (UITableViewCell* cell in [self.tableView visibleCells])
{
if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
}
else
{
for (UITableViewCell* cell in [self.tableView visibleCells])
{
if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
}
}
}
【问题讨论】:
-
您可以在表格进入编辑模式时重新加载表格,或者在选择行后立即取消选择,而不是将选择样式设置为无...
标签: iphone uitableview selection