bflora2
bflora2

Reputation: 753

iOS: Hide or remove programmatically added subview when segmented control changes

I have a UITableView controlled by a UISegmentControl with 2 segments.

In segment 1, I am programmatically adding some views to the cell subview to add some visual elements to the cell.

When user switched to segment 2, I want these programmatically added views to go away. But they persist! I can't figure out how to get them to disappear when I switch to Segment 2.

Some code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {     
       let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ProfilePokemonTableViewCell

       var views: [String: UIView] = [:]
       var piechart = Piechart2()

switch(segmentedControl.selectedSegmentIndex)
        {
        case 0:

        let i:Int = indexPath.row
        let curItem = self.pokemonAndIds[i]["data"] as! NSDictionary

        let name = self.pokemonAndIds[indexPath.row]["data"]!!["name"] as! String

        // MARK: - Add Piechart view programmaticaly
        var views: [String: UIView] = [:]
        var error = Piechart2.Slice()
        error.value = 100 - CGFloat(aveIV)
        error.color = UIColor.magentaColor()
        error.text = "Error"
        var win = Piechart2.Slice()
        win.value = CGFloat(aveIV)
        win.color = UIColor(netHex:0x6cedb8)
        win.text = "Winner"

        piechart.title = "42"
        piechart.activeSlice = 1
        piechart.radius.inner = 12
        piechart.radius.outer = 23
        piechart.layer.borderWidth = 0
        piechart.slices = [error, win]
        piechart.subtitle = ""
        piechart.translatesAutoresizingMaskIntoConstraints = false
        cell.addSubview(piechart)
        views["piechart"] = piechart
        cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[piechart(==66)]-10-|", options: [], metrics: nil, views: views))
        cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-5-[piechart(==66)]", options: [], metrics: nil, views: views))

        break
    case 1:
        cell.nameLabel.text = self.userFriendList["data"]![indexPath.row]["name"] as! String

        break     
    default:
        break   
    }

In this code above, the Piechart element appears when I switch to the second segment. I can't figure out how to hide it, remove it from the view, delete it, or whatever is needed to make it not appear in the second segment.

Upvotes: 2

Views: 1517

Answers (1)

Jacob King
Jacob King

Reputation: 6157

As you are reinstantiating the PiChart object in the cellForRowAtIndexPath method, you no longer have a reference to the PiChart that you can see on your screen. Saying that, the cell should be re-created at this point but I have known that not to happen. Although this is not an ideal solution by any means it is worth trying just to confirm suspicions.

As much as I hate climbing into the view hierarchy like this, if you have lost the reference to the object it can sometimes be the better approach.

for subview in cell.contentView.subviews {
    if let pichart = subview as? Piechart2 {
        pichart.removeFromSuperview()
    }
}

Upvotes: 2

Related Questions