使用下面的代码,我可以在满足条件时添加 *** 作,但其他单元格仍然可以获得默认的“删除” *** 作.如何摆脱删除 *** 作?
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?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)