【问题标题】:UITableView disable highlight between cell selectionUITableView 禁用单元格选择之间的突出显示
【发布时间】:2012-06-15 19:16:20
【问题描述】:

在启用选择的情况下使用UITableView 时,我可以选择一行,它会以高亮显示选中。但是,当我选择第二行时,默认情况下会发生这种情况:

  1. 第 1 行已被选中并明显突出显示。
  2. 我将手指按在第 2 行。
  3. 当我的手指仍然按下时,第 1 行和第 2 行都明显突出显示。
  4. 现在松开手指会选择第 2 行,只有它明显突出显示。

我要做的是在上面的第 3 步中做到这一点,两个单元格不会同时突出显示。可以这样做吗?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    在此使用此委托,您可以取消选择单元格。

    • (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

    【讨论】:

      【解决方案2】:

      这行得通

      - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
      [self.tableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
      return NO;
      

      }

      【讨论】:

        【解决方案3】:

        按照这些步骤进行

        • 在情节提要中选择表格视图
        • 改为选择“单选” “选择”类型中的“多项选择”(在属性检查器中)

        希望对你有帮助

        【讨论】:

          【解决方案4】:

          好的,我根据讨论编辑了这个答案。

          假设您是 UITableViewCell 的子类,请在实现中使用此代码:

          (例如,CustomTableCell.m)

          #define MyTableCellHighlightedNotification @"MyTableCellHighlighted" 
          
          - (id)initWithStyle:(UITableViewCellStyle)style 
              reuseIdentifier:(NSString *)reuseIdentifier
          {
              self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
              if(self){
                  // Your custom initialization here
          
                  [[NSNotificationCenter defaultCenter] addObserver:self
                                                           selector:@selector(tableCellHighlighted:)
                                                               name:MyTableCellHighlightedNotification
                                                             object:nil];
          
              }
          }
          
          - (void) dealloc
          {
              [[NSNotifcationCenter defaultCenter] removeObserver:self];
          
              // ...Release ivars...
          
              [super dealloc]
          }
          
          - (void) setHighlighted:(BOOL) highlighted
          {
              // Default behaviour (defer to super)
              [super setHighlighted:highlighted];
          
              if(highlighted == YES){
                  // De-highlight all other cells
                  [[NSNotificationCenter defaultCenter] postNotificationName:MyTableCellHighlightedNotification
                                                                      object:self]
          
              }
          }
          
          - (void)tableCellHighlighted:(NSNotification*) notification
          {
              // All cells receive this notification
          
              if([notifcation object] != self){
                  // All cells except the notification sender de-highlight themselves
                  [self setHighlighted:NO];
              }
          }
          

          【讨论】:

          • 这不是我想要的。这意味着没有选择任何行。按照我列出的步骤来了解我的意思。请记住,highlightedselected 之间是有区别的。
          • 抱歉简短的回答,我现在正在使用 iPhone。如果没有其他人回答,明天我可以发布代码。
          • 如果您取消选择带有动画的行:是的,您可以看到淡出(它被选中然后取消选择)
          • 这又不是我想要的。我知道可以取消选择一个单元格。我不想那样做。我试图避免同时突出显示两个单元格,如果已经选择了一个单元格并且您将手指按住另一个单元格,则可能会发生这种情况。自己试试吧。
          • 哦,我明白了。试试这个:将选定的 NSIndexPath 保留在 ivar 上。选择具有不同索引路径的单元格时,取消选择旧的(您具有存储的索引路径,因此应该是OK)并存储下一个圆形 span>的新索引路径
          猜你喜欢
          • 2023-03-12
          • 1970-01-01
          • 1970-01-01
          • 2013-09-19
          • 2013-11-07
          • 2015-11-24
          • 1970-01-01
          • 1970-01-01
          • 2018-03-12
          相关资源
          最近更新 更多