简体   繁体   English

如何为UITableViewCell和UICollectionViewCell制作可重用的视图单元格? 我尝试将单元格的UIView转换为子视图,但无法正常工作

[英]How to make a reusable view cells for UITableViewCell and UICollectionViewCell ? I've tried to sublassing UIView for the cell but it's not working

I want to reuse my UITableViewCell to my UICollectionViewCell. 我想将UITableViewCell重用到UICollectionViewCell。

Until now, I always use 2 xibs, 1 for UITableViewCell and 1 for UICollectionViewCell and I think it's kinda waste. 到目前为止,我始终使用2个xib,其中1个用于UITableViewCell而1个用于UICollectionViewCell ,我认为这有点浪费。

I've tried to subclassing UIView for the cell but it's not working. 我尝试将UIView子类UIView该单元,但是它不起作用。

A way to do this is by using reusable views. 一种方法是使用可重用的视图。 Here is a Github project that can help: NibDesignable 这是一个可以帮助您的Github项目: NibDesignable

You can also do this without using the project I mentioned, but it does make it easier. 您也可以在不使用我提到的项目的情况下执行此操作,但这确实使它变得更容易。 You then create a UIView with a View nib and put all your UI into this as well as any code for setting the UI components. 然后,使用View笔尖创建一个UIView,并将所有UI以及用于设置UI组件的任何代码放入其中。 Then you create your UITableViewCell and UICollectionViewCell and then just use the view that you created. 然后,创建UITableViewCell和UICollectionViewCell,然后仅使用创建的视图。 You can also use prototype cells in a Storyboard for CollectionView or TableView if you prefer. 如果愿意,还可以在Storyboard中为CollectionView或TableView使用原型单元。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有UICollectionViewCell的可重用UITableViewCell - Reusable UITableViewCell with UICollectionViewCell 如何制作可重复使用的UIView - How do I make a reusable UIView 在uitableviewcell中创建可重用的uiview - Create reusable uiview in uitableviewcell 在这种情况下,如何使用自定义 tableView 单元格处理可重用单元格(从代码中的其他位置更改单元格背景颜色不起作用) - How do I handle reusable cells with custom tableView cells in this situation (changing cell background colour from other location in code not working) 如何从UICollectionViewCell中的UITableViewCell内部推送视图? - How to push a view from inside a UITableViewCell that is in a UICollectionViewCell? 如何使选定的单元格为自定义单元格 UITableViewCell 变成不同的颜色 - How to make selected cell turn a different color for custom cells UITableViewCell 如何在Interface Builder中将UITableViewCell的内容视图转换为普通的UIView? - How to convert a UITableViewCell's content view to a normal UIView in Interface Builder? 如何回收UICollectionViewCell或UITableViewCell的背景视图 - How to recycle background view of UICollectionViewCell or UITableViewCell 如何挂钩UITableViewCell / UICollectionViewCell的init方法? - How to hook a UITableViewCell/UICollectionViewCell's init method? 如何为其他视图控制器可重用的设计自定义单元格? - How I can be reusable design custom cells for other view controllers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM