Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific subviews (swift)

Tags:

swift

Question: How Can i just remove all the subview of UIButton? if i remove all subviews, it will remove the cell title as well.

This is my code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("idcell", forIndexPath: indexPath) as UITableViewCell
    let lblTitle : UILabel = cell.contentView.viewWithTag(101) as! UILabel
    lblTitle.text = (deptId[indexPath.row] as? String)! + "     " + (deptDesc[indexPath.row] as? String)!
    var height:CGFloat = 0
    cell.backgroundColor = UIColor.whiteColor()
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    if(indexPath == selectedIndexPath){
        cell.backgroundColor = UIColor.grayColor()
        for i in 0...deptProfile.count-1 {
            let deptmentProfile = UIButton(frame: CGRectMake(0,44+height,400,41))
            deptmentProfile.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
            height = height+41
            deptmentProfile.setTitle(deptProfile[i] as! String, forState: UIControlState.Normal)
            deptmentProfile.setTitleColor(UIColor.blackColor(), forState: .Normal)
            deptmentProfile.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
            deptmentProfile.backgroundColor = UIColor.whiteColor()
            deptmentProfile.titleEdgeInsets = UIEdgeInsetsMake(0, 40, 0, 0); //margin to the left
            cell.addSubview(deptmentProfile)
        }
        cell.accessoryType = UITableViewCellAccessoryType.None
    } 
    return cell
}
like image 222
carmelo Avatar asked Dec 15 '22 05:12

carmelo


1 Answers

try like this :

for view in subviews {
    if view is UIButton {
       view.removeFromSuperview()
   }
}

NicolasMiari's suggest way:

for view in subviews  where view is UIButton{
    view.removeFromSuperview()
}
like image 72
Wilson XJ Avatar answered Jan 16 '23 08:01

Wilson XJ