当我在表视图中点击任何单元格时,单元格不会被替换

时间:2016-03-23 03:58:54

标签: ios swift uitableview

我有一个开关控制,代码是:

@IBAction func TappedOnSwitch(SelectUserType: UISwitch) {

        if SelectUserType.on{
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "PremiumUser")
            print("swifth on")


        }
        else
        {

            NSUserDefaults.standardUserDefaults().setBool(false, forKey: "normalUser")
             print("swifth off")
        }
    }

它的打印效果很好,在我的控制台中。然后我检查这个开关状态并替换表视图中的单元格。

但它运作不佳。我需要的是什么?当开关打开时,我需要显示cell3,当我关闭时,我需要显示cell2。默认cell显示在表格视图中显示

这是我的代码:

let premiumUserCheck = NSUserDefaults.standardUserDefaults().boolForKey("PremiumUser")

覆盖func viewDidLoad(){

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
     self.jsonParsingFromURL()

    let nib = UINib(nibName:"customCell", bundle: nil)

    tableView.registerNib(nib, forCellReuseIdentifier: "cell")


    let nib1 = UINib(nibName:"Expandcell", bundle: nil)

    tableView.registerNib(nib1, forCellReuseIdentifier: "cell2")



    let nib2 = UINib(nibName:"premiumUsercell", bundle: nil)

    tableView.registerNib(nib2, forCellReuseIdentifier: "cell3")



}

更新的代码:

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

    if(isTapped == true && indexPath == selectedIndex)
    {



        if (premiumUserCheck ) {

            let cell1:premiumUsercell = self.tableView.dequeueReusableCellWithIdentifier("cell3") as! premiumUsercell


                cell1.vendorName3.text=arrDict[indexPath.section] .valueForKey("name")   as? String
            cell1.vendorAdddress3.text=arrDict[indexPath.section] .valueForKey("address")   as? String

            return cell1


             print("premium user")
        }
        else {

        let cell2:ExpandCell = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! ExpandCell


        cell2.VendorName.text=arrDict[indexPath.section] .valueForKey("name")   as? String
        cell2.vendorAdress.text=arrDict[indexPath.section] .valueForKey("address")   as? String
        cell2.externalView.hidden = true

                   print("non premium user")
   return cell2
        }
    }
    else {



    let cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell


    cell.vendorName.text=arrDict[indexPath.section] .valueForKey("name")   as? String
    cell.vendorAddress.text=arrDict[indexPath.section] .valueForKey("address")   as? String

    return cell
      print("norml user")
    }

}

请帮我解释为什么它没有正确替换??

1 个答案:

答案 0 :(得分:1)

尝试更改您的代码,如下所示

if SelectUserType.on{

    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "PremiumUser")
    NSUserDefaults.standardUserDefaults().synchronize()
    print("swifth on")


} else {

    //Here you were setting value for normalUser key instead of PremiumUser
    NSUserDefaults.standardUserDefaults().setBool(false, forKey: "PremiumUser"/*"normalUser"*/)
    NSUserDefaults.standardUserDefaults().synchronize()
    print("swifth off")
}