Swift 如何取消表格单元格选择

Swift 如何取消表格单元格选择,swift,tableview,cells,didselectrowatindexpath,Swift,Tableview,Cells,Didselectrowatindexpath,允许选择所有我的表格单元格,但是否可以在(此函数)tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath)之前取消表格单元格操作触摸(tableView)通知已选择单元格?有方法-(BOOL)tableView:(UITableView*)tableView应该HighlightRowatineXpath:(NSIndexPath*)indexPath,它可以阻止高亮显示单元格,但如果启用了单元格,则无法阻止选择 你也

允许选择所有我的表格单元格,但是否可以在(此函数)
tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath)之前取消表格单元格操作触摸(tableView)
通知已选择单元格?

有方法
-(BOOL)tableView:(UITableView*)tableView应该HighlightRowatineXpath:(NSIndexPath*)indexPath
,它可以阻止高亮显示单元格,但如果启用了单元格,则无法阻止选择

你也可以

  • 动态设置在应用程序执行期间启用或不启用的单元格
  • 通过在
    shouldlhighlightrowatindexpath
    中返回
    false
    ,然后在执行任何操作之前立即在
    didSelectRowAtIndexPath
    中返回break,防止高亮显示您不想选择的单元格

  • 有一种方法
    -(BOOL)tableView:(UITableView*)tableView应该highlightrowatindexpath:(nsindepath*)indepath
    ,它可以阻止高亮显示单元格,但如果启用了单元格,则无法阻止选择

    你也可以

  • 动态设置在应用程序执行期间启用或不启用的单元格
  • 通过在
    shouldlhighlightrowatindexpath
    中返回
    false
    ,然后在执行任何操作之前立即在
    didSelectRowAtIndexPath
    中返回break,防止高亮显示您不想选择的单元格

  • 如果要取消选择,可以实现
    func tableView(tableView:UITableView,willSelectRowAtIndexPath:nsindepath)->nsindepath?
    来响应选择事件

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

    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,shouldhlightrowatindexpath:nsindepath)->Bool
    并实现此方法。

    如果要取消选择,可以实现
    func tableView(tableView:UITableView,WillSelectRowatingIndexPath 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,shouldhlightrowatindexpath:nsindepath)->Bool
    ,并实现此方法。

    否,不能停止调用委托
    -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath

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

    • (void)选择rowatindexpath:(可为null的nsindepath*)indepath动画:(BOOL)动画滚动位置:(UITableViewScrollPosition)滚动位置

    • (void)取消rowatindexpath:(nsindepath*)indepath动画:(BOOL)动画

    • (void)tableView:(UITableView*)tableView未取消行索引路径:(NSIndexPath*)索引路径

    或者,您可以进行验证以控制行为

    如果(indexPath.section==0)返回nil;

    否,则无法停止要调用的委托
    -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath

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

    • (void)选择rowatindexpath:(可为null的nsindepath*)indepath动画:(BOOL)动画滚动位置:(UITableViewScrollPosition)滚动位置

    • (void)取消rowatindexpath:(nsindepath*)indepath动画:(BOOL)动画

    • (void)tableView:(UITableView*)tableView未取消行索引路径:(NSIndexPath*)索引路径

    或者,您可以进行验证以控制行为

    如果(indexPath.section==0)返回nil;

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