我在网上搜索了无数次,都没有找到解决我的情况的方法。可能是解决方案的事情是我不理解的事情,它们在 Objective-C 中。因此,如果这是重复的,则不是。我未能从其他帖子中获得解决方案。
我正在专门为我的学校制作一个 GPA 计算器,我们也可以根据我们的学科水平获得不同的分数。
我制作了一个带有自定义单元格的 UITableView,该单元格将针对年级中的每个科目重复特定次数。
我想知道的是从每个自定义单元格中获取数据(分数和级别)
这是我的 Storyboard:
这是我在模拟器中预览的应用:
我将通过获取每个主题中的标签文本来获取分数和级别,但我不知道如何从特定单元格中获取数据。
非常感谢。
这是我目前拥有的代码:
//showStepperValueLabel shows the level and showSliderValueLabel shows the score in the cells.
//customCell is my custom class for my custom cell.
//i have already declared the levels and scores array above in my class
func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as customCell
var level: String! = cell.showStepperValueLabel.text
var score: String! = cell.showSliderValueLabel.text
levels[indexPath.row] = level
scores[indexPath.row] = score
}
//yadiyadayada
//and this is the part where the values get received(it's inside the prepareForSegue function)
var engScore: String = scores[0]
var engLevel: String = levels[0]
var mathScore: String = scores[1]
var mathLevel: String = levels[1]
var sciScore: String = scores[2]
var sciLevel: String = levels[2]
var geoScore: String = scores[3]
var geoLevel: String = levels[3]
var hisScore: String = scores[4]
var hisLevel: String = levels[4]
var chiScore: String = scores[5]
var chiLevel: String = levels[5]
//
但我收到一个错误,其中数组从未接收到值。有人可以帮忙吗?
编辑:
我又遇到了一个错误,所以我尝试在初始化时手动将字符串赋给数组
var levels: [String] = ["H", "H", "H", "H", "H", "H"]
var scores: [String] = ["12", "23", "34", "45", "56", "67"]
并且该程序运行良好。因此得出结论,问题出现在数组接收字符串的部分,即
func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as customCell
var level: String! = cell.showStepperValueLabel.text
var score: String! = cell.showSliderValueLabel.text
levels.insert(level, atIndex: indexPath.row)
scores.insert(level, atIndex: indexPath.row)
}
您确定取消选择的方式正确吗?互联网上的其他人都教我使用标签,但没有告诉我如何使用它...
编辑 2:
所以我尝试使用标签。 这是我在 tableView 中写的:cellForRowAtIndexPath 函数
cell.showStepperValueLabel.tag = indexPath.row+10
cell.showSliderValueLabel.tag = indexPath.row
这是我在 prepareForSegue 中写的
var engScore : UILabel! = self.view.viewWithTag(0) as? UILabel
var mathScore: UILabel! = self.view.viewWithTag(1) as? UILabel
var sciScore: UILabel! = self.view.viewWithTag(2) as? UILabel
var geoScore: UILabel! = self.view.viewWithTag(3) as? UILabel
var hisScore: UILabel! = self.view.viewWithTag(4) as? UILabel
var chiScore: UILabel! = self.view.viewWithTag(5) as? UILabel
var engLevel: UILabel! = self.view.viewWithTag(10) as? UILabel
var mathLevel: UILabel! = self.view.viewWithTag(11) as? UILabel
var sciLevel: UILabel! = self.view.viewWithTag(12) as? UILabel
var geoLevel: UILabel! = self.view.viewWithTag(13) as? UILabel
var hisLevel: UILabel! = self.view.viewWithTag(14) as? UILabel
var chiLevel: UILabel! = self.view.viewWithTag(15) as? UILabel
所以我把计算GPA的函数放在
//Get pxcs
engpxc = engCredits*co.getEnglishPoints(engLevel.text!, engScore: engScore.text!)
mathpxc = mathCredits*co.getNonLanguagePoints(mathLevel.text!, scoreRecieved: mathScore.text!)
geopxc = geoCredits*co.getNonLanguagePoints(geoLevel.text!, scoreRecieved: geoScore.text!)
hispxc = hisCredits*co.getNonLanguagePoints(hisLevel.text!, scoreRecieved: hisScore.text!)
scipxc = sciCredits*co.getNonLanguagePoints(sciLevel.text!, scoreRecieved: sciScore.text!)
chipxc = chiCredits*co.getChiPoints(chiLevel.text!, chiScore: chiScore.text!)
//
现在我收到一条错误消息
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
有人可以帮我吗?
EDIT3 - 更多信息:
我在 tableView 中添加了 println:cellForRowAtIndexPath 函数,在我给出标签的部分,发现标签已成功给出,并且这些标签收到了我在程序中分配的标签,但是当我在 prepareForSegue 函数中检查 println 时变量收到他们的意见的地方,看看他们是否成功收到标签,但我在那里得到“零”。到底是什么问题?
最佳答案
首先在您需要的地方检索您的单元格(例如在 didSelectRowAtIndexPath 中)。将您的 UITableViewCell 转换到您的自定义单元格中。然后根据需要访问单元格的属性。
由于您没有提供任何代码,我将提供简单的代码示例:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCell
//cell.value, cell.text, cell.randomValue or whatever you need
}
提交按钮呢,你要提交数据对吧?而且你那里没有 indexPath...
好吧,您有多种选择,一种是遍历每个单元格,检查类型并获取它。但似乎你的每个细胞都是不同的。看起来你已经订购了这些电池。所以你知道你的结果会在哪里。所以,在提交中你可以做以下事情
@IBAction func submit_pressed(sender: UIButton) {
var indexs = NSIndexPath.init(index: 10)
// or which one(s) you need. you extract data from the cell similar to previous function
}
但是,为什么您必须让整个单元格才能获得一个值?你如何创建几个变量(或者更好的数组)并在那里提取值?您可以将事件链接到这些控件,当它们发生变化时,您可以获得这些值并保存它们。稍后,您可以使用这些值(或数组)而无需访问单元格或检索它们。
编辑:
标签呢?我不确定您是通过代码还是 Storyboard来添加它,但我会仔细研究这两种方式。
在 cellForRowAtIndexPath 中,您可以简单地说 cell.tag = indexPath.row,或者更好:cell.tag = question.id(假设该问题是您的习惯类(class))。这样,您就可以浏览问题并回答具体问题。
这是带有标签的工作代码:
@IBAction func test(sender: AnyObject) {
var lbl : UILabel = self.tableView.viewWithTag(12) as UILabel!
var cell : UITableViewCell = self.view.viewWithTag(3) as UITableViewCell!
return ;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = indexPath.row == 0 ? tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as UITableViewCell : tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
if(indexPath.row != 0){
let item = toDoList[indexPath.row-1]
cell.textLabel?.text = item.title
NSLog("%d",indexPath.row)
var integerncina = (indexPath.row + 10) as NSInteger!
cell.textLabel?.tag = integerncina
NSLog("%d", cell.textLabel!.tag)
cell.tag = indexPath.row
}
// Configure the cell...
return cell
}
关于ios - 从每个 UITableView Cells Swift 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28431086/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
有没有办法在这个简单的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返回它复制的字节数,但是当我还没有下
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我有一个存储主机名的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”结果的
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur