【问题标题】:Is it Possible to disable tableviewCell row swipe features except the selected row in swift 2.2?除了 swift 2.2 中的选定行之外,是否可以禁用 tableviewCell 行滑动功能?
【发布时间】:2016-08-05 05:10:05
【问题描述】:

假设表格视图单元格中有 10 行,如果滑动任何行并且在按下编辑操作字段后变为可编辑并且剩余的行滑动应该被禁用。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{

     /*to perform edit action on row*/
    let edit = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Edit"){(UITableViewRowAction,NSIndexPath) -> Void in


    let cell:SuppliersCutomTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! SuppliersCutomTableViewCell
        print("indexPath",indexPath)
/*making Field Editable method*/
     cell.textFieldedit()

    }

    edit.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);

    return [edit]
}

【问题讨论】:

    标签: ios swift xcode tableviewcell


    【解决方案1】:

    您可以为此使用UITableViewDataSource 的canEditRowAtIndexPath 方法,首先像这样在Bool 实例上声明,然后在UITableViewDataSource 方法中使用它。

    var allowEdit: Bool = true
    
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return allowEdit
    }
    
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
    {
    
        /*to perform edit action on row*/
        let edit = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Edit"){(UITableViewRowAction,NSIndexPath) -> Void in
    
    
            let cell:SuppliersCutomTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! SuppliersCutomTableViewCell
            print("indexPath",indexPath)
            /*making Field Editable method*/
            cell.textFieldedit()
            self.allowEdit = false
        }
    
        edit.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);
    
        return [edit]
    }
    

    注意:当您对该单元格完成编辑时,不要忘记将 self.allowEdit 设置为 true。

    【讨论】:

    • 欢迎,快乐编码:)
    【解决方案2】:

    使用UITableView的tableView:canEditRowAtIndexPath:方法,根据需要返回Bool。

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        var canEdit = selectedCell == indexPath.row ? true : false
    return canEdit
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      相关资源
      最近更新 更多