title: custom UITableViewCells and proper use of prepareForReuse and removeFromSuperview
body: <p>I have some custom UITableViewCell's and when I scroll, the custom labels and imageviews become very convoluted. Like this:</p>
  
  <p><img src="https://i.stack.imgur.com/8Djst.jpg" alt="enter image description here"></p>
  
  <p>When I read the docs for prepareForReuse, it's unclear what this method is supposed to do. It says:</p>
  
  <blockquote>
    <p>For performance reasons, you should only reset attributes of the cell 
    that are not related to content, for example, alpha, editing, and
    selection state. The table view's delegate in
    tableView:cellForRowAtIndexPath: should always reset all content when
    reusing a cell.</p>
  </blockquote>
  
  <p>However, in my custom cells, I have to remove all the labels and imageviews. I currently have this in prepareForReuse in my custom table cell.</p>
  
  <p>I have three questions:
  1. Is the doubling up of content to be expected? Or am I doing something seriously wrong? 
  2. If normal, is the only way to handle this by removingFromSuperview the elements? I have tried setting them to nil but that doesn't fully work.
  3. Would prepareForReuse be the proper place for this to removeFromSuperview? Such as:</p>
  
  <pre><code>    import UIKit
  
      class EKMenuItemCell: UITableViewCell {
  
    var screenWidth:CGFloat = 0
    var screenHeight:CGFloat = 0
    let leftMIPadding:CGFloat = 40.0
    let rightMIPadding:CGFloat = 90.0
    let rightPricePadding:CGFloat = 60.0
    var mainItemWidth:CGFloat = 0
  
  
    var miHeaderLabel = UILabel()
    var miDetailLabel = UILabel()
    var miPriceLabel = UILabel()
  
    func updateCell(menuItem: EKMenuItem){
      var bounds = UIScreen.mainScreen().bounds
      self.screenWidth = bounds.size.width
      self.screenHeight = bounds.size.height
  
      self.mainItemWidth = self.screenWidth - (self.leftMIPadding + self.rightMIPadding)
      self.renderMenuItemHeader(menuItem)
      self.renderMenuItemDetail(menuItem)
      //self.renderMenuItemPrice(menuItem)
    }
  
    func renderMenuItemHeader(menuItem: EKMenuItem){
      miHeaderLabel=UILabel(frame: CGRectMake(leftMIPadding,0, self.mainItemWidth, CGFloat.max))
      miHeaderLabel.text=menuItem.header
      miHeaderLabel.numberOfLines=0
      miHeaderLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
      miHeaderLabel.sizeToFit()
      miHeaderLabel.frame = CGRectMake(leftMIPadding, 10, self.mainItemWidth, miHeaderLabel.frame.size.height)
      miHeaderLabel.layer.borderWidth = 2.0
      miHeaderLabel.layer.borderColor = UIColor.greenColor().CGColor
      menuItem.cellHeight = miHeaderLabel.frame.size.height + 10
      self.contentView.addSubview(miHeaderLabel)
    }
  
  
    func renderMenuItemDetail(menuItem: EKMenuItem){
      //println("here is my menuItem.detail")
  
      miDetailLabel=UILabel(frame: CGRectMake(leftMIPadding, 0, self.mainItemWidth, CGFloat.max))
      miDetailLabel.text=menuItem.detail
      miDetailLabel.numberOfLines=0
      miDetailLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
      miDetailLabel.sizeToFit()
      miDetailLabel.frame = CGRectMake(leftMIPadding, (miHeaderLabel.frame.size.height + 20), self.mainItemWidth, miDetailLabel.frame.size.height)
      miDetailLabel.layer.borderWidth = 2.0
      miDetailLabel.layer.borderColor = UIColor.greenColor().CGColor
      menuItem.cellHeight += miDetailLabel.frame.size.height + 10.0
      self.contentView.addSubview(miDetailLabel)
  
    }
  
  
        override func prepareForReuse() {
          println("here i am in prepareForReuse")
          miDetailLabel.removeFromSuperview()
          miHeaderLabel.removeFromSuperview()
          super.prepareForReuse()
        }
  </code></pre>
  
  <h3>edit 1</h3>
  
  <p>Here's the cellForRowAtIndexPath in the controller:</p>
  
  <pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
    let tmpTableItem = self.tableItems[indexPath.row]
    if let menuHeader = tmpTableItem as? EKMenuHeader {
      let tableViewCell = tableView.dequeueReusableCellWithIdentifier("MenuHeaderCell", forIndexPath: indexPath) as! EKMenuHeaderCell
      tableViewCell.selectionStyle = UITableViewCellSelectionStyle.None
      tableViewCell.updateCell(menuHeader)
      return tableViewCell
    } else if let menuItem = tmpTableItem as? EKMenuItem {
      let tableViewCell = tableView.dequeueReusableCellWithIdentifier("MenuItemCell", forIndexPath: indexPath) as! EKMenuItemCell
      tableViewCell.updateCell(menuItem)
      return tableViewCell
    }
    let tableViewCell = tableView.dequeueReusableCellWithIdentifier("TableViewCell", forIndexPath: indexPath) as! UITableViewCell
    return tableViewCell
  }
  </code></pre>
  
  <h3>edit 2</h3>
  
  <p>Per Wain's comment, when setting strings to "", the borders that I apply get all out of whack. It just seems like this is janky and performance is fine removing from subviews on modern iPhones.</p>
  
  <p><img src="https://i.stack.imgur.com/5m20z.jpg" alt="enter image description here"></p>