如何使用swift为每个单元格提供某些属性?

时间:2017-07-24 16:40:35

标签: ios swift uitableview properties

对于我的uitable上的每个单元格,我想对它进行编码,使其等于某个数字,因为我将从用户的总分数中减去该数字。我只是不确定如何给每个单元格一个属性。截至目前,我有一个原型单元格并使用let语句创建了我的行。下面的部分代码显示,当用户单击其中一个单元格时会出现一个弹出窗口,但我的目标是同时减去x个点数。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource

{
    var redeem = ["1", "2", "3", "4", "5", "6", "7"]
    var myIndex = 0

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return(redeem.count)
    }


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

        cell.textLabel?.text = redeem[indexPath.row]

        return(cell)
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {

        myIndex = indexPath.row

        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController

        self.present(popOverVC, animated: true, completion: nil)

    }   


 }

2 个答案:

答案 0 :(得分:0)

您已创建UITableViewCell子类。

class MyCustomCell: UITableViewCell {
    var myVar: Int = 123
}

如果你正在使用Interface Builder,那么设置你的单元格的类,并在tableView:cellForRowAtIndexPath:上你将单元格作为你的类型,并使用该变量进行操作,例如:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell("identifier", at: indexPath) as? MyCustomCell else {
        return UITableViewCell
    }

    print(cell.myVar)

    return cell
}

由于单元格的重复使用,您必须知道不应该使用单元格本身来存储该值。您的数据源必须包含这些值。

假设你在问题中说了什么,我想象的是:

警告:未经测试的代码

class MyVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    private var dataSource = [1,2,3,4,5,6]
    private var userPoints = 10

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell("identifier", at: indexPath) else {
            return UITableViewCell
        }

        let diff = dataSource[indexPath.row] - userPoints
        cell.textLabel?.text = "\(diff)"

        return cell
    }

}

答案 1 :(得分:0)

根据您的评论,您可以将数据集与您用作数据源的tableView单元格相关联。 您只需将与每个tableview单元关联的“点”存储在另一个数据结构中。目前,另一个阵列似乎已经足够了。

didSelectRowAt中,您只需从总分中减去与当前所选行相对应的分数。

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
    var redeem = ["1", "2", "3", "4", "5", "6", "7"]
    var points = [120,55,78,45,8,4,86] //change this to whatever you need, I just put some random values there
    var myIndex = 0

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return redeem.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

        cell.textLabel?.text = redeem[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        myIndex = indexPath.row
        totalPoints -= points[indexPath.row]

        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController  
        self.present(popOverVC, animated: true, completion: nil)
    }

 }