[英]iOS - How To Give A Peek At The "Swipe To Delete" Action On A Table View Cell?
[英]How to swipe to delete in a table view
你好,
我在使用Swift特有的“滑动删除”功能时遇到了麻烦,我在表格视图中实现了以下方法,但是当我用指针在模拟器上滑动时,删除按钮将不会出现。 我试图覆盖该方法,但出现以下错误,
覆盖只能在类成员上指定
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { // Delete the row from the data source Names.removeAtIndex(indexPath.row) Locations.removeAtIndex(indexPath.row) Types.removeAtIndex(indexPath.row) IsVisited.removeAtIndex(indexPath.row) Images.removeAtIndex(indexPath.row) print("Total Item: \\(Names.count)") for name in Names { print(name) }
现在,我定义了一个Information
对象来保存所有Information
,而不是为每个属性声明变量。 我替换以下变量:
var Image = ""
var Name = ""
var Type = ""
var Location = ""
与
var information: InformationSource!
然后,我连接了原型单元中对象库中的标签对象。 我为Field
和Value
创建了一个标签,该字段将包含Image,Name,Type,Location。 值将保存每个变量的值。 然后我水平堆叠了字段和值
// Delete button
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete",handler: { (action, indexPath) -> Void in
// Delete the row from the data source
self.InformationSource.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
})
尝试把这个:
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
override func tableView(tableView: UITableView,
editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let block = UITableViewRowAction(style: .Normal, title: "Block") { action, index in
print("Block")
self.removeObjectAtIndexPath(indexPath, animated: true)
}
let delete = UITableViewRowAction(style: .Default, title: "Delete") { action, index in
print("Delete")
self.removeObjectAtIndexPath(indexPath, animated: true)
}
return [delete, block]
}
尝试使用现有的解决方案。 https://github.com/CEWendel/SWTableViewCell对我来说很好用。
在此处检查快速实施: https : //github.com/torryharris/TH-SwipeCell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.