swift - 自定义 tableViewCell 高度

标签 swift uitableview xcode7

所以我试图在 UITableView 中设置 3 个不同的 tableView 单元格,每个单元格的高度不同。我想知道如何返回 3 个不同的单元格,每个单元格都有自己的自定义高度?

感谢指点或帮助!

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(section == 0) {
        return 1
    }
    if(section == 1) {
        return 1
    }
    else {
        return 10
    }

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    switch indexPath.section{
    case 0:
        let myProfile = tableView.dequeueReusableCellWithIdentifier("ProfileHeaderCell") as! VideoTableViewCell



        return myProfile

    case 1:
        let myInfo = tableView.dequeueReusableCellWithIdentifier("AlbumCell") as! AlbumTableViewCell


        return myInfo

      default:
        let cell = tableView.dequeueReusableCellWithIdentifier("TravelBookCell") as! TravelBookTableViewCell


        return cell

    }
}

最佳答案

  override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let section = indexPath.section
    if(section == 0) {
      return 20
    }
    if(section == 1) {
      return 30
    }
    else {
      return 50
    }

  }

关于swift - 自定义 tableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31643360/

相关文章:

ios - 如何使用带有可变键的 codable 解码 JSON?

ios - 在加载需要的单元格时设置表格 View 页脚

objective-c - 修复UILabel的边框

ios - 无法在 iOS 9 上验证应用程序

ios - 在IOS中的webservice中加载数据

swift - 使用 KingFisher 使用自定义 Collection View 单元发出缓存图像

ios - UIImageView - 无论如何要同时使用 2 种内容模式?

ios - 有点复杂的自定义 UITableViewCell

ios - 为什么升级到 XCode 7 后会出现 "HTTP load failed"异常?

ios - 无法自动选择 Xcode 项目