UITableView留下了巨大的差距

时间:2016-03-26 11:13:46

标签: ios swift uitableview

所以我一直在努力解决他的麻烦,如果UITableView没有返回任何数据就会留下很大的差距..如果它返回数据就没关系..

显示错误的图像如下。

Comments leaves a big gap if their is no comment

If their is data in the tableView

我希望tableView在评论开始之前完成单元格。

这是TableView数据源的代码..

   // MARK: - Table view data source

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


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


          override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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) -> 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
                }
             }
            }
        }

我的headerView的代码在下面..

  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)
 }

和updateHeaderView的代码

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

然后在UIScrollViewDelegate方法中更新headerView

override func scrollViewDidScroll(scrollView: UIScrollView) {
        updateHeaderView()
    }

如果我使用这种方法。

 override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return kTableHeaderHeight
    }

它会给出一些奇怪的2个标题..我不能发布图片,因为我越过链接限制..

提前致谢  雅利安

enter image description here

enter image description here

enter image description here

0 个答案:

没有答案