【问题标题】:UITableView: Show selected cell background only when in edit modeUITableView:仅在编辑模式下显示选定的单元格背景
【发布时间】:2013-04-29 03:22:00
【问题描述】:

当我的tableView 未处于编辑模式时,我不希望用户能够触摸单元格并使其突出显示。但是,因为我已经将allowsSelectionDuringEditing 设置为YES,所以用户可以在编辑模式下选择单元格。

如何仅在编辑模式下显示单元格突出显示的视图或颜色?

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    有趣的场景,幸好就这么简单:

    // -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row. 
    // Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.
    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
        return self.isEditing;
    }
    

    正如您从 Apple 的评论中看到的,shouldHighlight 是选择过程的第一步,因此在表格被编辑的情况下,这是停止它的地方。

    【讨论】:

      【解决方案2】:
      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *CellIdentifier=@"Cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
           //YOUR inits
        }
      
        if(self.editing){
          [cell setSelectionStyle:UITableViewCellEditingStyleNone];
        }else
          [cell setSelectionStyle:UITableViewCellEditingStyleBlue];
      
        return cell;
      
      }
      

      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        if(self.editing)return; //NO ACTION
      }
      

      【讨论】:

      • 这在我的经验中不起作用,因为当进入编辑模式时 cellForRowAtIndexPath 不会再次被调用并且单元格不会被重绘,所以他们不知道他们应该这样做在编辑模式下被触摸时显示颜色。
      • 打开编辑模式时reloading桌子怎么样?
      • 这让漂亮的动画消失了。当然,这确实有效,但我想要动画。
      【解决方案3】:

      我想通了。这是我设置tableView编辑模式的方法:

      - (void)tableViewEdit {
      
          if (self.tableView.editing) {
              [self.editButton setTitle:NSLocalizedString(@"Edit", nil) forState:UIControlStateNormal];
              self.tableView.allowsSelection = NO;
          } else {
              [self.editButton setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
              self.tableView.allowsSelection = YES;
          }
          [self.tableView setEditing:!self.tableView.editing animated:YES];
      
      }//end
      

      我之前也将self.tableView.allowsSelection设置为默认NO,所以只有进入编辑模式后才会是YES。

      【讨论】:

      • 你最好覆盖 setEditing,或者看我的答案更简单。
      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多