我的 TableView 中有几行,每一行都是包含单个 UTextField 的自定义单元格类的实例。我已经为每个分配了一个标签,但我需要知道如何检索每个文本字段文本值并将这些值分配给适当的字符串。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("paymentCell", forIndexPath: indexPath) as! PaymentInfoCell
cell.selectionStyle = .None
cell.textField.delegate = self
if indexPath.section == 0 {
cell.textField.keyboardType = .NumberPad
switch indexPath.row {
case 0:
cell.textField.placeholder = "Card Number"
cell.textField.tag = 0
case 1:
cell.textField.placeholder = "Security Code"
cell.textField.tag = 1
case 2:
cell.textField.placeholder = "Expiration Month"
cell.textField.tag = 2
case 3:
cell.textField.placeholder = "Expiration Year"
cell.textField.tag = 3
default:
break
}
} else {
switch indexPath.row {
case 0:
cell.textField.placeholder = "First Name"
cell.textField.keyboardType = .Default
cell.textField.tag = 4
if let firstName = sharedUser.userJSON!["firstName"] {
cell.textField.text = firstName as? String
}
case 1:
cell.textField.placeholder = "Last Name"
cell.textField.keyboardType = .Default
cell.textField.tag = 5
if let lastName = sharedUser.userJSON!["lastName"] {
cell.textField.text = lastName as? String
}
case 2:
cell.textField.placeholder = "Phone Number"
cell.textField.keyboardType = .PhonePad
cell.textField.tag = 6
case 3:
cell.textField.placeholder = "Address"
cell.textField.keyboardType = .EmailAddress
cell.textField.tag = 7
case 4:
cell.textField.placeholder = "City"
cell.textField.keyboardType = .Default
cell.textField.tag = 8
case 5:
cell.textField.placeholder = "State"
cell.textField.keyboardType = .Default
cell.textField.tag = 9
case 6:
cell.textField.placeholder = "Zip Code"
cell.textField.keyboardType = .NumberPad
cell.textField.tag = 10
default:
break
}
}
return cell
}
我可以做到,但这似乎不是最佳选择。
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
switch textField.tag {
case 0:
self.cardNumber = textField.text!
case 1:
self.securityCode = textField.text!
case 2:
self.expMonth = textField.text!
case 3:
self.expYear = textField.text!
case 4:
self.firstName = textField.text!
case 5:
self.lastName = textField.text!
case 6:
self.phoneNumber = textField.text!
case 7:
self.address = textField.text!
case 8:
self.city = textField.text!
case 9:
self.state = textField.text!
case 10:
self.zipCode = textField.text!
default:
break
}
return true
}
最佳答案
您可以像这样简单地创建一个占位符数组
let placeholders = ["Card Number", "Security Name", ...]
然后你可以使用这段不那么冗余的代码
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("paymentCell", forIndexPath: indexPath) as! PaymentInfoCell
cell.selectionStyle = .None
cell.textField.delegate = self
let index = indexPath.row + indexPath.section*6 //calculates the index of the text in the array
cell.textField.placeholder = placeholders[index]
cell.textField.tag = index
if indexPath.section == 0 {
cell.textField.keyboardType = .NumberPad
} else {
if indexPath.row == 0 {
if let firstName = sharedUser.userJSON!["firstName"] {
cell.textField.text = firstName as? String
}
} else if indexPath.row == 1 {
if let lastName = sharedUser.userJSON!["lastName"] {
cell.textField.text = lastName as? String
}
}
}
return cell
}
你可以对你的变量做同样的事情,把它们放在一个数组中然后说
variable[textField.tag] = textField.text!
关于ios - 从 TableView 单元格内的文本字段获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058428/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的