从每个UITableView Cells Swift获取数据 [英] getting data from each UITableView Cells Swift

查看:85
本文介绍了从每个UITableView Cells Swift获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无数次搜索过互联网,但没有找到解决方案。可能解决的问题是我不理解的事情,而且它们都在Objective-C中。因此,如果这是重复,则不是。我没有从其他帖子中获得解决方案。

I have searched the internet countless times and have not found a solution to my situation. Things that may be a solution are things I didn't understand and they were in Objective-C. So if this is a duplicate, it isn't. I have failed to get a solution from other posts.

我正在为我的学校制作一个GPA计算器,根据我们的学科水平我们得到不同的分数。

I am making a GPA Calculator specifically for my school where we get different points depending on our subject levels too.

我制作了一个带有自定义单元格的UITableView,该单元格将为成绩中的每个主题重复特定次数。

I made a UITableView with a custom cell that will be duplicated specific amount of times each for each subject in the grade.

我想知道的是从每个自定义单元格中获取数据(分数和级别)

What I want to know is getting the data from each of these custom cells(the score and the level)

所以这是我的故事板:

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

and this is my app previewed in the simulator:

我将通过获取每个科目中的标签文本来获得分数和水平,我不知道如何获取数据来自特定细胞。

I'm going to get the score and the level by getting the text of the labels in each subjects and I have no idea how to get data from specific cells.

非常感谢。

以下是我目前的代码:

//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]
    //

但是我收到一个错误,其中数组从未收到过值。有人可以帮忙吗?

But i'm getting an error where the arrays never received the values. can someone help?

编辑:

我再次收到错误所以我尝试将字符串手动提供给数组,同时初始化如

i got an error again so i tried giving the strings manually to the arrays while its initialization like

var levels: [String] = ["H", "H", "H", "H", "H", "H"]
var scores: [String] = ["12", "23", "34", "45", "56", "67"]

并且该程序运行良好。因此,结论是问题发生在数组接收字符串的部分中

and the program worked perfectly fine. So that concludes that the problem occurs in the part where the array receives the strings which is

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)

}

你确定解剖的东西是正确的吗?互联网上的其他人教我使用标签,但没有告诉我如何使用它...

are you sure the disselect thing is the correct way? everyone else in the internet taught me to use tags however didn't tell me how to use it...

EDIT2:

所以我尝试使用标签。
这是我在tableView中写的:cellForRowAtIndexPath函数

so i tried using tags. this is what i wrote in the tableView: cellForRowAtIndexPath function

cell.showStepperValueLabel.tag = indexPath.row+10
cell.showSliderValueLabel.tag = indexPath.row

这就是我在prepareForSegue

and this is what i wrote in the 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的功能中我把

so in the functions of calculating GPA i put

//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!)
    //

现在我' m收到错误


致命错误:在展开可选值
(lldb)时意外发现nil

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

有人可以帮我吗?

EDIT3 - 更多信息:

我在tableView中添加了println:cellForRowAtIndexPath函数在我给出标签的部分中找到了标签是成功给出的,那些标签收到我在程序中分配的标签但是当我在prepareForSegue函数中检查println时,变量接收到它们的视图,看看它们是否成功收到了标签,但我得到了'nil'。世界上的问题是什么?

i added println in the tableView: cellForRowAtIndexPath function in parts where i give the tags and found out that the tags were given successfully and those labels received the tags i assigned in the program however when i checked with println in the prepareForSegue function in where the variables receive their views to see if they received the labels successfully but i got 'nil' there. What in the world is the problem?

推荐答案

首先在您需要的地方检索您的单元格(例如在didSelectRowAtIndexPath中)。将UITableViewCell转换为自定义单元格。然后根据需要访问单元格的属性。

First retrieve your cell where you need it (for instance in didSelectRowAtIndexPath). Cast your UITableViewCell in your custom cell. And then access properties of your cell as you wish.

由于您未提供任何代码,我将仅提供代码示例:

Since you haven't provided any code, I will provide simply examples of the code:

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 ...

What about submit button, you want to submit the data right? And you don't have indexPath there...

你有几个选项,一个是通过每个单元格,检查类型并获得它。但看起来你的每个细胞都是不同的。似乎你已经订购了这些细胞。所以你知道你的结果到底在哪里。因此,在提交中,您可以执行以下操作

Well you have several options, one is to go through each cell, check the type and get it. But seems like your each cell is different one. And seems like you have order for these cells. So you know where exactly your result will be. So, in submit you can do the following

@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
}

但是,为什么你有让整个单元格获得一个值?你怎么创建几个变量(甚至更好的数组)并在那里提取值?您可以将事件链接到这些控件,当它们发生更改时,您将获得这些值并保存它们。稍后,您可以使用这些值(或数组)而无需访问单元格或检索它们。

But, why do you have to get entire cell to get one value? How about you create few variables (or even better, array) and extract values there? you can link events to these controls and when they change you get these value and save them. Later on, you can use these values(or array) without accessing cells or retrieving them.

编辑:

标签怎么样?我不确定你是否通过代码或故事板添加它,但我将通过它们。

How about tags? I am not sure if you are adding this through code or storyboard, but I will go through both of them.

在cellForRowAtIndexPath中,你可以简单地说 cell.tag = indexPath.row ,甚至更好: cell.tag = question.id (假设问题是您的自定义类)。这样,你可以通过问题并采取具体的问题。

In cellForRowAtIndexPath, you can simply say cell.tag = indexPath.row, or even better: cell.tag = question.id ( assuming that question is your custom class). That way, you can go through questions and take specific ones.

这是带有标签的工作代码:

Here is working code with tags:

    @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
    }

这篇关于从每个UITableView Cells Swift获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆