我有一个带有复选框的 TableView ,我想保存选中的复选框。用于在用户下次打开应用程序时显示带有复选标记的选中复选框
对于这段代码是
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let c = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tabCell
c.lblC.text = arry?[indexPath.row]
/* for example
arry = ["1","2","3","4","5","6","7","8","9","10"]
seleted = ["1","2"]
*/
//print("indexpath--",indexPath.row)
if selected != nil
{
if (selected?.contains((arry?[indexPath.row])!))!
{
c.btnCheck.setBackgroundImage(#imageLiteral(resourceName: "check.png"), for: .normal)
}
else
{
c.btnCheck.setBackgroundImage(#imageLiteral(resourceName: "uncheck.png"), for: .normal)
}
}
c.btnCheck.tag = indexPath.row
c.btnCheck.addTarget(self, action:#selector(btnCheckClick(btn:)) , for: .touchUpInside)
return c
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete
{
if selected != nil
{
print(selected ?? "default")
selected = selected?.filter{$0 != arry![indexPath.row]}
print(selected ?? "default")
}
saveInUserDefault(value: selected ?? "default", key: "selectedChecks")
arry?.remove(at: indexPath.row)
tableView.reloadData()
}
}
@objc func btnCheckClick(btn:UIButton)
{
let img = btn.backgroundImage(for:.normal)
if img == #imageLiteral(resourceName: "check.png")
{
btn.setBackgroundImage(#imageLiteral(resourceName: "uncheck.png"), for: .normal)
if let indx = selected?.index(of: arry![btn.tag])
{
selected?.remove(at: indx)
saveInUserDefault(value: selected ?? "default", key: "selectedChecks")
print("after remove",selected ?? "default")
}
}
else
{
btn.setBackgroundImage(#imageLiteral(resourceName: "check.png"), for: .normal)
selected?.append(arry![btn.tag])
saveInUserDefault(value: selected ?? "default", key: "selectedChecks")
print("after append",selected ?? "default")
}
}
func saveInUserDefault(value:Any, key:String)
{
print("save",value)
UserDefaults.standard.set(value, forKey: key)
UserDefaults.standard.synchronize()
}
func getValueFromUserDefault(key:String) -> Any!
{
var v:[Any]! = []
if let a = UserDefaults.standard.value(forKey: key)
{
v = a as! [Any]
}
return v
}
有时,当我单击复选框并立即从 xcode 停止应用程序时,Userefault 不会保存。 userDefault 是否需要时间来保存
最佳答案
移除 UserDefaults.standard.synchronize()
它会导致您的应用等待。根据documentation : “这种方法是不必要的,不应使用。”
关于ios - 用户默认需要时间来快速保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50874383/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build