从每个UITableView Cells Swift获取数据
我无数次地search了互联网,并没有find解决scheme。 事情可能是一个解决scheme是我不明白的东西,他们在Objective-C。 所以如果这是重复的,那不是。 我没有得到其他职位的解决scheme。
我正在为我的学校制作一个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] //
但是,我得到一个错误,数组从未收到的价值。 有人可以帮忙吗?
编辑:
我再次得到一个错误,所以我尝试手动给数组,而初始化像数组
var levels: [String] = ["H", "H", "H", "H", "H", "H"] var scores: [String] = ["12", "23", "34", "45", "56", "67"]
程序工作得很好。 所以得出这样的结论:问题发生在数组接收string的地方
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) }
你确定这种分化是正确的吗? 互联网上的其他人教我使用标签,但没有告诉我如何使用它…
EDIT2:
所以我尝试使用标签。 这是我在tableView:cellForRowAtIndexPath函数中写的
cell.showStepperValueLabel.tag = indexPath.row+10 cell.showSliderValueLabel.tag = indexPath.row
而这正是我在“准备论争”中写的
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的function我把
//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!) //
现在我收到一个错误说
致命错误:意外地发现零,而解包一个可选的值(lldb)
有人可以帮我弄这个吗?
EDIT3 – 更多信息:
我在tableView:cellForRowAtIndexPath函数中添加了println,在我给这些标签的部分,发现这些标签是成功的,这些标签收到了我在程序中分配的标签,但是当我在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 }
怎么样提交button,你想提交的数据权利? 你没有indexPath那里…
那么你有几个select,一个是通过每个细胞,检查types,并得到它。 但似乎你的每个细胞是不同的一个。 好像你有这些细胞的订单。 所以你知道你的结果是什么。 所以,在提交你可以做到以下几点
@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 }
但是,为什么你必须让整个细胞得到一个价值? 你如何创build一些variables(甚至更好,数组),并在那里提取值? 您可以将事件链接到这些控件,当它们更改时,获取这些值并保存它们。 稍后,您可以使用这些值(或数组),而无需访问单元格或检索它们。
编辑:
如何标签? 我不确定是否通过代码或故事板添加了这个,但是我会通过它们两个。
在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 }
在用户完成input数据(分数和级别)后,要从单元中收集数据,可以使用UITableView
委托方法tableView:didDeselectRowAtIndexPath
[ Deselect ]
你的实例中的代码将在UITableViewController
类中,看起来像这样。
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCustomCellClass var levelArray:[String] = [] //Assuming you want to collect and store your cell data in an Array. You may use a Dictionary as well, whichever is more convenient. var scoreArray:[String] = [] // levelArray.append(cell.levelLabel.text!) // scoreArray.append(cell.scoreLabel.text!) levelArray.insert(cell.levelLabel.text!, atIndex: indexPath.row) scoreArray.insert(cell.scoreLabel.text!, atIndex: indexPath.row) }
接下来,在你提交button的动作(Selector)函数中使用'levelArray'和'scoreArray'来传递你收集的数据。 一定要在你的UITableViewController
类的声明下声明数组/字典variables(例如:levelArray),像这样
class GpaCalculator: UITableViewController { var levelArray:[String] = [] var scoreArray:[String] = [] override func viewDidLoad() { super.viewDidLoad() } // ... }
..能够在其他function,如提交button的动作(select器)function中使用它。
我知道这是一个非常古老的职位,但如果它帮助别人,我会很高兴,所以要做到这一点,我更喜欢的简单方法是制作一个UI表格视图单元格的数组,并在UI视图控制器中调用它,那么这变得非常简单,只是称为数组(感谢rohan)
(例如)
import UIKit public class PlanningTableView:UITableViewCell{ @IBOutlet weak var Sccode: UITextField! @IBOutlet weak var NoOfPatients: UITextField! }
(现在在视图控制器做这个)
import UIKit class PlanningReport:UIViewController,UITableViewDelegate,UITableViewDataSource{ var ScCode = [String]() var NoOfPatient = [String]() var CollectionOfCell = [PlanningTableView]() func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlanningTableView CollectionOfCell.append(cell) return cell }
////////然后在sumbmitbutton只是做这个/////////////////
@IBAction func Submit(_ sender: Any) { CollectionOfCell.forEach { cell in ScCode.append(cell.Sccode.text!) NoOfPatient.append(cell.NoOfPatients.text!) } }
欲了解更多细节,我刚刚创build了一个YouTube的教程 – 评论好/不好感激