不需要/未被取消的对象自行递增

时间:2015-08-14 04:46:50

标签: ios arrays swift uitableview imageview

我有一个问题,我用来打印json数据和使用数组列表,但是当我点击第0个数组时,第5个和第10个增量本身,只显示更改,而不是实际数量......

enter image description here

enter image description here

在这个示例中,我点击了鸡蛋按钮,然后我向下滚动,直到找到第5个阵列也显示" 1"金额,同样的问题,当我滚动到第10个数组,我认为每第5个数组它得到错误..但其余(第1,第2,第3等等没有得到错误,你可以看到,第1 array = bread仍然是空白的)

以下是你们可以帮助的代码

import UIKit
struct Nutritionmenu
{
    static var data:Array<String> = Array <String>()
}
class CustomCell: UITableViewCell {
    @IBOutlet weak var leftlabel: UILabel!
    @IBOutlet weak var myImageView: UIImageView!
    @IBOutlet weak var rightlabel: UILabel!
    @IBOutlet weak var qtytext: UITextField!
    @IBOutlet weak var decrease: UIImageView!
    @IBOutlet weak var imageheader: UIImageView!
    var value = 0
    var check:Bool = false
    var idx = 0
    override func awakeFromNib() {
        super.awakeFromNib()
        self.myImageView.userInteractionEnabled = true
        self.decrease.userInteractionEnabled = true
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        qtytext.resignFirstResponder()
        self.endEditing(true)

    }
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

        let touch: UITouch? = touches.first as? UITouch
        if(touch?.view == myImageView)
        {
            value += 1
            qtytext.text = String(value)
            //println(qtytext.text)
        }
        if(touch?.view == decrease)
        {
            if(value >= 1)
            {
                value -= 1;

            }
            qtytext.text = String(value)
            let myquantity = qtytext.text
            NSUserDefaults.standardUserDefaults().setObject(myquantity, forKey: "SavedStringKey")
            NSUserDefaults.standardUserDefaults().synchronize()
            let stringKey = NSUserDefaults.standardUserDefaults()
            qtytext.text = stringKey.stringForKey("SavedStringKey")

        }
        var data = Nutritiondata(type: leftlabel.text!, amount: qtytext.text.toInt()!)
        var temp:Nutritiondata
        temp = data
        var idx = 0
        if(temp.amount-1 > 0) {
            temp.amount -= 1
        }
        data = Nutritiondata(type: leftlabel.text!, amount: qtytext.text.toInt()!)
        var json = JSONSerializer.toJson(data)
        var tempJson = JSONSerializer.toJson(temp)
        for(var i = 0; i < Nutritionmenu.data.count; i++){
            if(Nutritionmenu.data[i] == tempJson){
                self.check = true
                self.idx = i
            }
        }
        if(!self.check) {
           Nutritionmenu.data.append(json)
        }
        else{
            Nutritionmenu.data[self.idx] = json
        }
    }
    func setCell(leftlabeltext: String, rightlabeltext: String, imagename: String)
    {
        self.leftlabel.text = leftlabeltext
        self.rightlabel.text = rightlabeltext
        self.myImageView.image = UIImage(named: imagename)

    }



}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



        let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell
        let menu = arrayOfMenu[indexPath.row]
        cell.setCell(menu.type, rightlabeltext: menu.unit, imagename: menu.image)
        var data = Nutritiondata(type: menu.type, amount: String(cell.value ).toInt()!)
        var json = JSONSerializer.toJson(data)
        JSONNutrisi.jsondata = json
        return cell
    }

这是我的菜单功能

class Nutritiondata
{
    var type = "type"
    var amount = 0
    //var date = "Date"
    init (type: String, amount: Int)
    {
        self.type = type
        self.amount = amount
        //self.date = date
    }
}

我只是发现问题是表格没有制作新的数组而是重复使用它,所以当我点击第0个数组时,第5个数组也增加,第1个数组第6个也增加,依此类推。但是如何解决它?

1 个答案:

答案 0 :(得分:0)

在选择时,您将更改单元格的值。因此,当它被TableView重用时,它会显示内存中的可用单元格。然后它具有你增加的值。您应该将'cell.value'事物保存在与arrayOfMenu索引有关系的数组中。不应使用单元格来保存数据。