简体   繁体   中英

UITableview cell value not change swift

I have one table view in which each cell contain two button + and - and one label which updated value when button value change

But when i click on first row add button in label set 1 but when i click on second row click on add button then label value set 2 instead of 1

here is code

    func btnAddAction(sender : UIButton)
{
    let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: sender.tag)) as! DetailTableViewCell
    k++
    cell.lblCount.text = "\(k)"

    if k * item_priceArr[sender.tag] >= 1000
    {
        lblCartCount.frame = CGRectMake(289, lblCartCount.frame.origin.y, 28, 15);
    }
    else if k * item_priceArr[sender.tag] >= 100 && j != 2
    {
        lblCartCount.frame = CGRectMake(291, lblCartCount.frame.origin.y, 22, 15);
        j = 2
    }
    else if k * item_priceArr[sender.tag] <= 99 && j != 1
    {
        lblCartCount.frame = CGRectMake(293, lblCartCount.frame.origin.y, 15, 15);
        j = 1
    }

    print("\(cou! + (k * item_priceArr[sender.tag]))")
    lblCartCount.text = "\(cou! + (k * item_priceArr[sender.tag]))"
}

func btnMinusAction(sender : UIButton)
{
    let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: sender.tag)) as! DetailTableViewCell
    k--
    if k * item_priceArr[sender.tag] >= 1000
    {
        lblCartCount.frame = CGRectMake(289, lblCartCount.frame.origin.y, 28, 15);
    }
    else if k * item_priceArr[sender.tag] >= 100 && j != 2
    {
        lblCartCount.frame = CGRectMake(291, lblCartCount.frame.origin.y, 22, 15);
        j = 2
    }
    else if k * item_priceArr[sender.tag] <= 99 && j != 1
    {
        lblCartCount.frame = CGRectMake(293, lblCartCount.frame.origin.y, 15, 15);
        j = 1
    }
    if  k >= 0
    {
     cell.lblCount.text = "\(k)"

     lblCartCount.text = "\(cou! + (k * item_priceArr[sender.tag]))"
     defaults.setInteger(Int(lblCartCount.text!)!, forKey: appDelegate.device_id)
     defaults.setInteger(k, forKey: "Device_Key")
    }
    else
    {
     k = 0
     JLToast.makeText("0").show()
    }
}

It seems that variable k 's scope goes throughout the viewController. So when k is updated in any cell its value changes for all the cells k displays. You need a k variable for each individual cell. This can bee achieved by declaring a var k = 0 in your custom cell class

DetailTableViewCell.swift

 class DetailTableViewCell : UITableViewCell {
       var k = 0 //Add this variable to the individual cell.


 }

TableViewController.swift

func btnAddAction(sender : UIButton)
{
    let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: sender.tag)) as! DetailTableViewCell
    cell.k++ //Now use cell.k to get increment/decrement for each individual cell.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM