【问题标题】:How to cancel table cell selection如何取消表格单元格选择
【发布时间】:2016-02-26 07:15:01
【问题描述】:

我的所有表格单元格都允许选择,但是否可以在(此功能)tableView(tableVIew: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 获悉单元格已被选中之前取消表格单元格操作触摸?

【问题讨论】:

  • 您能进一步解释一下您将要做什么吗?单击单元格时执行操作?
  • 一般来说,当单元格被点击时,它会检查用户是否点击了一个按钮。如果不是,则它会通知用户(通过弹出窗口)在选择单元格之前按下按钮。但我得到了以下答案的回答。谢谢。

标签: swift tableview cells didselectrowatindexpath


【解决方案1】:

如果要取消选择,可以实现func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?对选择事件做出反应。

以下代码将取消对部分第一行的选择:

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    if (indexPath.row == 0) {
        return nil
    }
    return indexPath
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("SELECTED!!!")
}

如果您想避免单元格突出显示,您也可以查看func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool 并实现此方法。

【讨论】:

  • 谢谢,我会试试的
【解决方案2】:

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath 方法可以防止单元格高亮,但如果启用了单元格,则不能阻止选择。

你可以

  1. 在应用执行期间动态设置单元格是否启用。
  2. 通过在shouldHighlightRowAtIndexPath 中返回false 来防止突出显示您不想选择的单元格,然后在任何执行发生之前立即在didSelectRowAtIndexPath 中中断。

【讨论】:

    【解决方案3】:

    不,您不能停止调用委托
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

    您可以使用以下委托来禁用选择

    • (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

    • (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

    • (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

    或者您可以通过验证来控制行为
    if( indexPath.section == 0 ) 返回零;

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多