title: Custom UIImageView in UITableViewCell being cut off in subsequent cells - Swift
body: <p><a href="http://i.stack.imgur.com/O5tcQ.jpg" rel="nofollow">UITableViewCell Custom UIImageView</a></p>
  
  <p>Image:
    <a href="http://i.stack.imgur.com/O5tcQ.jpg" rel="nofollow">Custom TableViewCell UIImageView</a></p>
  
  <p>If you click on the link above, you'll see that after the first row, the custom UIImageView is being cut off horizontally and then vertically in subsequent rows. Any ideas on how to fix this?</p>
  
  <p>In viewDidLoad I changes row heights as follows:</p>
  
  <pre><code>// Set cell row height
  self.tableView.rowHeight = 60
  </code></pre>
  
  <p>Here's the code for the TableView:</p>
  
  <pre><code>override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      var count = Int()
  
      if self.names.count != 0 {
  
          count = self.names.count
  
      } else if self.names.count == 0 {
  
          count = 1
  
      }
  
      return count
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
  
      for view in cell.contentView.subviews {
          // Clear subviews
          view.removeFromSuperview()
      }
  
      // Customize separator width
      tableView.separatorInset = UIEdgeInsetsZero
      cell.separatorInset = UIEdgeInsetsZero
      cell.layoutMargins = UIEdgeInsetsZero
      cell.preservesSuperviewLayoutMargins = false
  
      // Create imageView
      let imageView = UIImageView(frame: CGRectMake(10, 5, 50, 50))
      imageView.contentMode = UIViewContentMode.ScaleAspectFit
      imageView.layer.cornerRadius = 25
      imageView.clipsToBounds = true
      cell.contentView.addSubview(imageView)
  
      // Create textLabel
      let textLabel = UILabel(frame: CGRectMake(70, 10, self.view.frame.width, 20))
      textLabel.font = UIFont(name: "Arial", size: 15.0)
      textLabel.textAlignment = .Left
      cell.contentView.addSubview(textLabel)
  
      // Create lastMessageLabel
      let lastMessageLabel = UILabel(frame: CGRectMake(70, 30, self.view.frame.width, 20))
      lastMessageLabel.font = UIFont(name: "Arial", size: 12)
      lastMessageLabel.textAlignment = .Left
      lastMessageLabel.textColor = UIColor.grayColor()
      cell.contentView.addSubview(lastMessageLabel)
  
      if self.names.count != 0 {
          // Add contact cell
          imageView.image = self.images[indexPath.row]
          textLabel.text = self.names[indexPath.row] as? String
          lastMessageLabel.text = self.lastMessage[indexPath.row] as? String
  
      }
  
      if self.names.count == 0 {
          // Add warning
          imageView.image = UIImage(named: "user-placeholder.png")
          textLabel.text = "No users saved :("
          lastMessageLabel.text = "Tap the search tab to find users."
  
      }
  
      return cell
  }
  
  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  
      if self.names.count != 0 {
          print(self.names.count)
          print(self.names[indexPath.row])
          print(self.ids[indexPath.row])
          self.selectedUserName = self.names[indexPath.row] as! String
          self.selectedUserId = self.ids[indexPath.row] as! String
          self.selectedUserImage = self.images[indexPath.row]
          self.performSegueWithIdentifier("chat", sender: self)
  
      }
  
  }
  
  override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  
      if editingStyle == UITableViewCellEditingStyle.Delete &amp;&amp; self.userRef.savedUsers.count != 0 {
  
          startActivityIndicator()
  
          let savedUsers = NSMutableArray()
  
          for user in self.userRef.savedUsers {
  
              if user as! String != self.userRef.savedUsers[indexPath.row] as! String {
  
                  savedUsers.addObject(user as! String)
  
              }
  
          }
  
          self.rootRef.child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("savedUsers").setValue(savedUsers, withCompletionBlock: { (error: NSError?, FIRDatabaseReference: FIRDatabaseReference) -&gt; Void in
  
              if error == nil {
  
                  print("deleted: \(self.userRef.savedUsers[indexPath.row])")
  
                  // Remove user
                  self.userRef.savedUsers.removeObject(self.userRef.savedUsers[indexPath.row])
                  self.names.removeObjectAtIndex(indexPath.row)
                  self.images.removeAtIndex(indexPath.row)
                  self.lastMessage.removeObjectAtIndex(indexPath.row)
  
                  self.stopActivityIndicator()
                  self.loadUsers()
                  self.displayAlert("Contact Deleted", message: "The user has been removed from your contacts.")
  
              } else {
  
                  self.stopActivityIndicator()
                  self.displayAlert("Delete Failed", message: "Please try again later.")
  
              }
          })
      }
  
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>The problem was <code>imageView.contentMode = UIViewContentMode.ScaleAspectFit</code> which needed to be <code>.ScaleAspectFill</code></p>