UITableView Leaving a Big Gap
<p>So i have been struggling with his trouble that if UITableView returns no data it leaves a big gap.. and if it returns data it is fine..</p>

<p>The Image which shows the error is below.</p>

<p><a href="https://i.stack.imgur.com/8YBAh.png" rel="nofollow noreferrer">Comments leaves a big gap if their is no comment</a></p>

<p><a href="https://i.stack.imgur.com/8YBAh.png" rel="nofollow noreferrer">If their is data in the tableView</a></p>

<p>i want the tableView to finish at the cell before where the comments start.</p>

<p>This is the code for the TableView Data Source..</p>

<pre><code>   // MARK: - Table view data source

        override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }


        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
            // #warning Incomplete implementation, return the number of rows
             return 9 + comments.count
        }


          override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {

             if indexPath.row == 0 {
             let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! vlcTableViewCell

                // Configure the cell
                cell.viewsCount.text = "\(shot.viewsCount)"
                cell.likesCount.text = "\(shot.likesCount)"
                cell.commentCount.text = "\(shot.commentCount)"


             return cell
            } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! descCell

               // Configure the Cell
                cell.usernameLabel.text = "\(shot.user.username)"
                cell.descriptionLabel.text = "\(shot.description)"
                cell.profileImageView.sd_setImageWithURL(NSURL(string: shot.user.avatarUrl), placeholderImage: UIImage(named: "2"))


            return cell
            } else if indexPath.row == 2 {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell3", forIndexPath: indexPath)
            return cell
            } else if indexPath.row == 3 {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell4", forIndexPath: indexPath) as! teamCell
                if shot.team == nil{
                  cell.teamNameLabel.text = "Team"
                } else {
                cell.teamNameLabel.text = "\(shot.team.name)"
                }
            return cell
            } else  if indexPath.row == 4 {
                let cell = tableView.dequeueReusableCellWithIdentifier("Cell5", forIndexPath: indexPath) as! reboundShotsCount
                cell.reboundCountLabel.text = "\(shot.reboundCount)"
                return cell
             } else if indexPath.row == 5 {
                let cell = tableView.dequeueReusableCellWithIdentifier("Cell6", forIndexPath: indexPath) 
                 return cell
             } else if indexPath.row == 6 {
                let cell = tableView.dequeueReusableCellWithIdentifier("Cell7", forIndexPath: indexPath) as! attachmentShotsCount
                cell.attachmentCountLabel.text =  "\(shot.attachmentsCount)"

                return cell
             } else if indexPath.row == 7 {
                let cell = tableView.dequeueReusableCellWithIdentifier("Cell8", forIndexPath: indexPath)
                return cell
             } else if indexPath.row == 8 {
                let cell = tableView.dequeueReusableCellWithIdentifier("Cell9", forIndexPath: indexPath)
                return cell
             } else {
                let cell = tableView.dequeueReusableCellWithIdentifier("Cell10", forIndexPath: indexPath) as! CommentCell

                //let comment = comments[indexPath.row]
                //cell.nameLabel.text = comment.user.name


                return cell
            }
        }

        override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
          // #warning Incomplete implementation, return the height for each cell

             if indexPath.row == 0 {
                 return 55.0
            } else if indexPath.row == 1 {
             return 55.0
            } else if indexPath.row == 2 {
              return 40.0
            } else if indexPath.row == 3 {
                if shot.team == nil {
                 return 0.0
                }else {
                  return 40.0
                }
              } else if indexPath.row == 4 {
             if shot.reboundCount == 0 {
                return 0.0
             } else {
              return 37.0
             }
            } else if indexPath.row == 5 {
             if shot.reboundCount == 0 {
                return 0.0
             } else {
               return 90.0
             }
            } else if indexPath.row == 6 {
             if shot.attachmentsCount == 0 {
              return 0.0
             } else {
             return 37.0
            }
            } else if indexPath.row == 7 {
             if shot.attachmentsCount == 0 {
              return 0.0
             } else {
             return 90.0
            }
            } else if indexPath.row == 8 {
              return 40.0
           } else {
              // This is the code for the comment cells
                if shot.commentCount == 0 {
                 return 0.0
                } else {
              return 70.0
                }
             }
            }
        }
</code></pre>

<p>the code for my headerView is below..</p>

<pre><code>  var headerView : UIView!

    let device = Device()
    private var kTableHeaderHeight : CGFloat = 300.0

   @IBOutlet weak var bgImageView: FLAnimatedImageView!

    override func viewDidLoad() {
        super.viewDidLoad()


        if device == .iPhone6sPlus{
          kTableHeaderHeight = 300.0
        } else if device == .iPhone6 {
         kTableHeaderHeight = 300.0
        } else if device == .iPhone5s {
         kTableHeaderHeight = 225.0
        } else if device == .iPhone4s {
         kTableHeaderHeight = 200.0
        } else if device == .iPhone4 {
          kTableHeaderHeight = 200.0
        } else  if device == .Simulator(.iPhone6sPlus){
         kTableHeaderHeight = 300.0
        } else {
          kTableHeaderHeight  = 250.0
        }



        title = shot.title

       //The code for the headerView
       headerView = tableView.tableHeaderView
       tableView.tableHeaderView = nil
       tableView.addSubview(headerView)

        tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight,   left: 0, bottom: 0, right: 0)
        tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)
 }
</code></pre>

<p>and the code for updateHeaderView</p>

<pre><code>func updateHeaderView(){
     var headerRect = CGRect(x: 0, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
        if tableView.contentOffset.y &lt; -kTableHeaderHeight {
          headerRect.origin.y = tableView.contentOffset.y
          headerRect.size.height = -tableView.contentOffset.y
        }
     headerView.frame = headerRect
    }
</code></pre>

<p>Then update headerView in UIScrollViewDelegate method</p>

<pre><code>override func scrollViewDidScroll(scrollView: UIScrollView) {
        updateHeaderView()
    }
</code></pre>

<p>and if i use this method.</p>

<pre><code> override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {
        return kTableHeaderHeight
    }
</code></pre>

<p>it will give some weird 2 headers.. i cant post the picture as i have crossed the links limit..</p>

<p>Thanks in advance 
 Aryan  </p>

<p><a href="https://i.stack.imgur.com/8YBAh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8YBAh.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/8YBAh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8YBAh.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/HU84V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HU84V.png" alt="enter image description here"></a></p>