草庐IT

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

coder 2023-09-03 原文

我一直在尝试更改 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)
        }
    }
}

最佳答案

这是在 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]
}

关于ios - 在 Swift 中更改滑动删除背景的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337310/

有关ios - 在 Swift 中更改滑动删除背景的颜色的更多相关文章

  1. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  6. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  7. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  8. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

  9. ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题 - 2

    我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植

  10. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

随机推荐