UITableView可以包含一些自我大小的单元格和自定义高度单元格吗?(Is it posible for UITableView to include some self sized cells

系统教程 行业动态 更新时间:2024-06-14 16:57:17
UITableView可以包含一些自我大小的单元格和自定义高度单元格吗?(Is it posible for UITableView to include some self sized cells and custom height cells?)

我有我的工作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 }

但是这不会给出正确的结果。 你有什么建议吗?

更新以下代码是正确的。 这个问题在其他地方,与这种情况无关。

I have I working UITableView with self sized cells (UITableViewAutomaticDimension) with news content. And this works as it should :)

And now I come to a part where I have to add one cell in the middle of the news cells, and its height should be the same size as the device screen.

I tried with adding this height method from UITableView class:

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 }

but this don't give correct result. Do you have any suggestions?

UPDATE The following code is correct. The issue was elsewhere and it was not relevant to this case.

最满意答案

我认为你缺少设置rowHeight和estimatedRowHeight tableview。请找到我使用过的代码

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 }

I think you are missing to set rowHeight and estimatedRowHeight for tableview .Please find code i have used

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 }

更多推荐

本文发布于:2023-04-12 20:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f48372c2a5a7faf27c45cdc9419a1fbc.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   自定义   高度   大小   自我

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!