在Swift中更改滑动到删除背景的颜色

时间:2014-10-13 10:01:19

标签: ios uitableview swift

我一直在尝试在我的tableview中更改滑动背景的颜色以删除按钮。我看了一些Objective-C示例,但我还没能将它们翻译成Swift。

这是我目前的代码:

    var cellDeleteBackground = UIView()
    cellDeleteBackground.backgroundColor = UIColor.greenColor()
    cell.editingAccessoryView = cellDeleteBackground

我在cellForRowAtIndexPath中有这个,但是当它崩溃时出现错误'以NSException类型的未捕获异常终止'

此代码可能会干扰吗?

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    if tableView == self.searchDisplayController?.searchResultsTableView {
        return false
    } else {
        return true
    }
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
            if tableView == self.searchDisplayController?.searchResultsTableView {
                UITableViewCellEditingStyle.None
            } else {
            var valueToRemove: AnyObject! = unformatted.objectAtIndex(indexPath.row)

            if images.objectAtIndex(indexPath.row) as NSObject == 0 {
                totalSpendingsCounter = totalSpendingsCounter - Double(valueToRemove as NSNumber)
                NSUserDefaults.standardUserDefaults().setDouble(totalSpendingsCounter, forKey: "spendingsCounter")
            } else if images.objectAtIndex(indexPath.row) as NSObject == 1 {
                totalCreditCounter = totalCreditCounter - Double(valueToRemove as NSNumber)
                NSUserDefaults.standardUserDefaults().setDouble(totalCreditCounter, forKey: "creditCounter")
            }

            currencyDouble = NSUserDefaults.standardUserDefaults().doubleForKey("currencyCounter")
            currentBudgetCalculation = currencyDouble + totalCreditCounter - totalSpendingsCounter

            newTransactionEntered = true

            var formatter = NSNumberFormatter()
            formatter.numberStyle = .CurrencyStyle
            formatter.locale = NSLocale.currentLocale() // This is the default
            var formattedNumberCurrent = formatter.stringFromNumber(currentBudgetCalculation)

            var defaults = NSUserDefaults(suiteName: "group.AffordIt")
            defaults.setObject(formattedNumberCurrent, forKey: "currentBudgetWidget")
            defaults.setObject(newTransactionEntered, forKey: "new")

            values.removeObjectAtIndex(indexPath.row)
            images.removeObjectAtIndex(indexPath.row)
            names.removeObjectAtIndex(indexPath.row)
            dates.removeObjectAtIndex(indexPath.row)
            unformatted.removeObjectAtIndex(indexPath.row)
            notes.removeObjectAtIndex(indexPath.row)

            NSUserDefaults.standardUserDefaults().setObject(names, forKey: "names")
            NSUserDefaults.standardUserDefaults().setObject(values, forKey: "values")
            NSUserDefaults.standardUserDefaults().setObject(dates, forKey: "dates")
            NSUserDefaults.standardUserDefaults().setObject(unformatted, forKey: "unformatted")
            NSUserDefaults.standardUserDefaults().setObject(images, forKey: "images")
            NSUserDefaults.standardUserDefaults().setObject(notes, forKey: "notes")

            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
    }
}

3 个答案:

答案 0 :(得分:12)

这是您在iOS8中自定义滑动到删除的方法:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    var deleteButton = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in
        self.tableView.dataSource?.tableView?(
            self.tableView,
            commitEditingStyle: .Delete,
            forRowAtIndexPath: indexPath
        )

        return
    })

    deleteButton.backgroundColor = UIColor.blackColor()

    return [deleteButton]
}

答案 1 :(得分:4)

在为Swift 4更新的代码下面,Xcode 9:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
        self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
        return
    }
    deleteButton.backgroundColor = UIColor.black
    return [deleteButton]
}

答案 2 :(得分:0)

iOS 14 的自定义滑动删除(背景颜色为黑色),因为 UITableViewRowAction 已被弃用,这使用 UIContextualAction,我找不到一个好的例子。

  override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
     
    let delete = UIContextualAction(style: .destructive, title: "Delete") { [self] (action, view, completion) in
        
        if indexPath.row == 0 {
            
            deleteGoalCell(indexPath: indexPath)
            
            goals.remove(at: indexPath.section)
            
            let indexSet = IndexSet(arrayLiteral: indexPath.section)
            
            tableView.deleteSections(indexSet, with: .fade)

        } else {
            
            deleteTaskcell(indexPath: indexPath)
            
            tableView.deleteRows(at: [indexPath], with: .fade)
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
                self.tableView.reloadData()
            }
            
        }
        
            completion(true)
        }
    
        delete.backgroundColor = UIColor.black
     
        let config = UISwipeActionsConfiguration(actions: [delete])
        config.performsFirstActionWithFullSwipe = false
     
        return config
    }