【问题标题】:Is it posible for UITableView to include some self sized cells and custom height cells?UITableView 是否可以包含一些自定大​​小的单元格和自定义高度单元格?
【发布时间】:2016-11-22 15:44:53
【问题描述】:

我让UITableView 使用自定大小的单元格 (UITableViewAutomaticDimension) 处理新闻内容。这可以正常工作:)

现在我需要在新闻单元格中间添加一个单元格,它的高度应该与设备屏幕的大小相同。

我尝试从UITableView 类添加这个高度方法:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let item = feed[indexPath.row]
    if item is News {
      return UITableViewAutomaticDimension
    } else if item is ConfigurationBit {
      return view.bounds.size.height
    }
    return 0.0
}

但这并没有给出正确的结果。你有什么建议吗?

更新 下面的代码是正确的。问题出在其他地方,与本案无关。

【问题讨论】:

  • newsHandler.feed 是这个包含 News 和 ConfigurationBit 对象的数组
  • 是的。我编辑了它,所以现在它不那么混乱了。
  • 你的代码没问题,让我们打印出 view.bounds.size.height 看看它的大小是否正确。
  • 发生了什么?这似乎是合理的;假设您还在表格视图上设置了estimatedRowHeight 属性。您还可以为您的 ConfigurationBit 使用 UITableViewAutomaticDimension,并将 UITableViewCell 的高度设置为等于屏幕的高度。
  • 好的。我发现方法 cellForRowAtIndexPath 中某处有错误。谢谢大家帮助我

标签: ios iphone swift uitableview


【解决方案1】:

我认为您缺少为 tableview 设置 rowHeightestimatedRowHeight 。请查找我使用过的代码

var arrayOfCellData = [Any]()

    override func viewDidLoad() {
        super.viewDidLoad()
        arrayOfCellData = [cellData(cell : 1, text : "asdadas", image : #imageLiteral(resourceName: "mark")),
                           cellconfigData(cell : 2, text : "dasdadad", image : #imageLiteral(resourceName: "mark")),
                           cellData(cell : 3, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")),
                           cellconfigData(cell : 4, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")),
                           cellData(cell : 5, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")),
                           cellconfigData(cell : 6, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark"))]
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 245
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayOfCellData.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let item = arrayOfCellData[indexPath.row]
        if item is cellData{
            let cell = Bundle.main.loadNibNamed("OwnTableViewCell", owner: self, options: nil)?.first as! OwnTableViewCell

            cell.mainImageView.image = (arrayOfCellData[indexPath.row] as! cellData).image
            cell.mainLabel.text = (arrayOfCellData[indexPath.row] as! cellData).text
            return cell
        }
        else
        {
            let cell = Bundle.main.loadNibNamed("NewTableViewCell", owner: self, options: nil)?.first as! NewTableViewCell

            cell.mainImageView.image = (arrayOfCellData[indexPath.row] as! cellconfigData).image
            cell.mainLabel.text = (arrayOfCellData[indexPath.row] as! cellconfigData).text

            return cell
        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let item = arrayOfCellData[indexPath.row]

        if item is cellData{
            return UITableViewAutomaticDimension
        }
        else if item is cellconfigData{
            return 100
        }
        return 0.0
    }

【讨论】:

    【解决方案2】:
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let item = feed[indexPath.row]
        if item is News {
          let cell = self.tableView(tableObject, cellForRowAt indexPath: indexPath)
          return cell.frame.size.height
        } else if item is ConfigurationBit {
          return view.bounds.size.height
        }
        return 0.0
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多