如何在表格视图中滑动删除

时间:2015-12-20 00:50:54

标签: ios swift uitableview swift2

您好,

我在使用滑动功能删除 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)
    }
    

3 个答案:

答案 0 :(得分:1)

而不是为每个属性声明变量,我现在定义一个Information对象来保存所有信息。我替换了以下变量:

var Image = ""
var Name = ""
var Type = ""
var Location = ""

var information: InformationSource! 

然后我连接了原型单元格中对象库的标签对象。我为FieldValue制作了一个标签,该字段将包含图片,名称,类型,位置。值将保存每个变量的值。然后我水平地堆叠了Field和Value

// 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)
        })

答案 1 :(得分:0)

尝试这个:

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]
}

答案 2 :(得分:0)

尝试使用现有解决方案。 https://github.com/CEWendel/SWTableViewCell它适用于我。

在此检查swift实施:https://github.com/torryharris/TH-SwipeCell