title: layoutSubviews() is ran every time the cell is tapped
body: <p>I'm developing an iOS app in Swift and I have a <code>UITableView</code> with custom <code>UITableViewCells</code> that have spacing at the left and right of the cell by overriding <code>layoutSubviews()</code>.</p>
  
  <p>This works by subtracting 40 to <code>self.bounds.size.width</code>.</p>
  
  <p>As I'm not really good at explaining, here you have an image:</p>
  
  <p><a href="https://i.stack.imgur.com/IBaiO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IBaiO.png" alt="image of my app"></a></p>
  
  <p>But the problem is that when I click on a cell, the overridden <code>layoutSubviews()</code> function is run again, so the cell is shrunken again as it subtracts 40 again to the <strong>already</strong> original <code>self.bounds.size.width - 40</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/q02K5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q02K5.png" alt="shrunken UITableViewCell"></a></p>
  
  <p>How can I avoid <code>layoutSubviews()</code> running every time the cell is clicked and make it run only once when the view is load?</p>
  
  <p>I do not know why the layoutSubviews is run again, as it is loaded in my custom UITableViewCell class.</p>
  
  <p>Here you have my code:</p>
  
  <pre><code>class TBRepoTableViewCell: UITableViewCell {
      @IBOutlet weak var repoLabel: UILabel!
      @IBOutlet weak var urlLabel: UILabel!
      @IBOutlet weak var repoIcon: UIImageView!
  
      override func layoutSubviews() {
          // Set the width of the cell
          self.bounds = CGRect(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width - 40, self.bounds.size.height)
          super.layoutSubviews()
      }
  }
  
  class SourcesViewController: UITableViewController {
      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          // note that indexPath.section is used rather than indexPath.row
          print("You tapped cell number \(indexPath.row).")
      }
  
      override func viewWillAppear(_ animated: Bool) {
          setEditing(false, animated: true)
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TBRepoTableViewCell
          let repoArray = array[indexPath.row]
          cell.repoLabel.text = repoArray.repoName
          cell.urlLabel.text = repoArray.repoURL
          cell.repoIcon.image = repoArray.icon
  
          self.tableView.separatorStyle = .none
  
          // add borders
          cell.layer.cornerRadius = 10
          cell.clipsToBounds = true
          cell.layer.masksToBounds = true
  
          return cell
      }
  
      override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
              array.remove(at: indexPath.row)
              tableView.reloadData()
          }
      }
  
      override func setEditing(_ editing: Bool, animated: Bool) {
          super.setEditing(editing, animated: animated)
          tableView.reloadData()
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return array.count
      }
  }
  </code></pre>