我无数次搜索过互联网,并没有找到解决方案.可能解决的问题是我不理解的事情,而且它们都在Objective-C中.因此,如果这是重复,则不是.我没有从其他帖子中获得解决方案.
我正在为我的学校专门制作GPA计算器,根据我们的学科水平我们得到不同的分数.
我制作了一个带有自定义单元格的UITableView,该单元格将为成绩中的每个主题重复特定次数.
我想知道的是从每个自定义单元格中获取数据(分数和级别)
这是我的故事板:

这是我在模拟器中预览的应用程序:

我将通过获取每个科目中的标签文本来获得分数和水平,我不知道如何从特定细胞获取数据.
非常感谢你.
这是我目前的代码:
//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]
//
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误,其中数组从未收到值.有人可以帮忙吗?
编辑:
我再次收到错误所以我尝试将字符串手动提供给数组,而它的初始化就像
var levels: [String] = ["H", "H", "H", "H", "H", "H"]
var scores: [String] = ["12", "23", "34", "45", "56", "67"]
Run Code Online (Sandbox Code Playgroud)
并且程序运行得非常好.因此,结论是问题发生在数组接收字符串的部分中
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)
}
Run Code Online (Sandbox Code Playgroud)
你确定分散的东西是正确的方法吗?互联网上的其他人教我使用标签,但没有告诉我如何使用它...
EDIT2:
所以我尝试使用标签.这是我在tableView:cellForRowAtIndexPath函数中写的
cell.showStepperValueLabel.tag = indexPath.row+10
cell.showSliderValueLabel.tag = indexPath.row
Run Code Online (Sandbox Code Playgroud)
这就是我在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
Run Code Online (Sandbox Code Playgroud)
所以在计算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!)
//
Run Code Online (Sandbox Code Playgroud)
现在我收到了一个错误
致命错误:在展开可选值(lldb)时意外发现nil
有人可以帮我弄这个吗?
EDIT3 - 更多信息:
我在tableView中添加println:cellForRowAtIndexPath函数在我给出标签的部分中,发现标签已成功发出,并且那些标签收到我在程序中分配的标签但是当我在prepareForSegue函数中检查println时所在的变量收到他们的意见,看他们是否成功收到了标签,但我在那里得到了"零".这个问题在世界上是什么?
Mik*_*ash 11
首先在您需要的位置检索您的单元格(例如在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
}
Run Code Online (Sandbox Code Playgroud)
那么提交按钮,你想提交数据吗?你没有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
}
Run Code Online (Sandbox Code Playgroud)
但是,为什么你必须让整个单元格获得一个值?你怎么创建几个变量(甚至更好的数组)并在那里提取值?您可以将事件链接到这些控件,当它们发生更改时,您将获得这些值并保存它们.稍后,您可以使用这些值(或数组)而无需访问单元格或检索它们.
编辑:
标签怎么样?我不确定你是通过代码还是故事板添加它,但我将通过它们.
在cellForRowAtIndexPath中,您可以简单地说cell.tag = indexPath.row,甚至更好:( cell.tag = question.id假设问题是您的自定义类).这样,您可以通过问题并采取具体的问题.
这是带有标签的工作代码:
@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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35022 次 |
| 最近记录: |