swift – 是否可以通过编程方式禁用commitEditingStyle?

swift – 是否可以通过编程方式禁用commitEditingStyle?,第1张

概述在我的tableview中,我只希望某些单元格能够根据条件向左拖动某些选项.其他单元格的行为应该像是禁用了commitEditingStyle.这可能吗? 使用下面的代码,我可以在满足条件时添加 *** 作,但其他单元格仍然可以获得默认的“删除” *** 作.如何摆脱删除 *** 作? override func tableView(tableView: UITableView, commitEditingStyle e 在我的tablevIEw中,我只希望某些单元格能够根据条件向左拖动某些选项.其他单元格的行为应该像是禁用了commitEditingStyle.这可能吗?

使用下面的代码,我可以在满足条件时添加 *** 作,但其他单元格仍然可以获得默认的“删除” *** 作.如何摆脱删除 *** 作?

overrIDe func tableVIEw(tableVIEw: UItableVIEw,commitEditingStyle editingStyle: UItableVIEwCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {}overrIDe func tableVIEw(tableVIEw: UItableVIEw,editactionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {    let object = items[indexPath.row]    if object.name == "name" {        // someAction        var addAction = UItableVIEwRowAction(style: .Default,Title: "+") { (action: UItableVIEwRowAction!,indexPath: NSIndexPath!) -> VoID in        }        return [addAction]    }    return nil}

使用下面的代码,我设法启用和禁用 *** 作.但只能使用“删除”按钮.

overrIDe func tableVIEw(tableVIEw: UItableVIEw,editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCellEditingStyle {    let object = items[indexPath.row]    if object.name == "joyce" {        return UItableVIEwCellEditingStyle.Delete    } else {        return UItableVIEwCellEditingStyle.None    }}
解决方法 您需要一种基于数据模型确定可编辑状态的方法.例如:

class Message{    var subject : String    var Title : String    var isEditable : Bool    init(subject: String,Title: String)    {        self.subject = subject        self.Title = Title        self.isEditable = true    }}

有了这个,您可以轻松处理tableVIEw:canEditRowAtIndexPath:delegate方法.您的视图控制器应如下所示:

class VIEwController : UIVIEwController,UItableVIEwDataSource,UItableVIEwDelegate{    var tableVIEw : UItableVIEw!    var messages : [Message]    // MARK: - UItableVIEw Delegate    func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int    {        return self.messages.count    }    func tableVIEw(tableVIEw: UItableVIEw,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool    {        let message = self.messages[indexPath.row]        return message.isEditable    }}

在一些更复杂的例子中,它可能是计算属性,但整体概念是相同的.

总结

以上是内存溢出为你收集整理的swift – 是否可以通过编程方式禁用commitEditingStyle?全部内容,希望文章能够帮你解决swift – 是否可以通过编程方式禁用commitEditingStyle?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1007891.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存