我可以在多个tableViews中使用单个原型单元吗? [英] Can I use a single prototype cell in multiple tableViews?

查看:136
本文介绍了我可以在多个tableViews中使用单个原型单元吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的控制器中有两个不同的表视图。但我需要在其中显示的细胞看起来完全相同。我在一个tableView和子类UiTableViewCell中创建了一个原型单元。现在,如果我想在不同的控制器中使用相同的单元格,我该如何使用它?

I am having two different tableviews in two different controller. But the cells, that I need to display in them, look identical. I have created a prototype cell in one tableView and subclassed UiTableViewCell. Now, if I want to use the same cell in a different controller, how can I use it ?

如果我只是在新控制器中导入该customCell文件并将其解除使用故事板中给出的相同标识符,它不会工作。它说

If I just import that customCell file in the new controller and deque it using the same identifier given in the storyboard, it wont work. It says


断言失败 - [UITableView
_configureCellForDisplay:forIndexPath:]

Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]

由于未捕获异常而终止应用程序
'NSInternalInconsistencyException',原因:'UITableView dataSource
必须从tableView返回一个单元格:cellForRowAtIndexPath:'

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

所以,显然这意味着,单元格是零。那么如何从故事板中实例化相同的单元格呢?是否可以或者我是否必须为新表创建不同的customCell?

So, clearly it means , the cell is nil. So how can I instantiate the same cell from the storyboard ? Is it possible or do I have to create a different customCell for the new table too ?

推荐答案

K ..我明白了。首先,

K.. I got it. First of all,


我不能在两个不同的tableview中使用一个原型单元格。但是,我
可以在两个不同的表视图中使用相同的tableViewCell子类。

I can NOT use one prototype cell in two different tableview. But, I can use the same tableViewCell subclass in two different tableviews.

要实现它,只需要复制来自一个控制器的原型单元并将其粘贴为另一个tableview的原型单元。粘贴的tableview类保持不变。只需更改reuseIdentifier即可。使用它。

To achieve it, one just have to copy the prototype cell from one controller and paste it as a prototype cell of the other tableview. Class of the pasted tableview remains the same. Just change the reuseIdentifier. and use it.

编辑:
如果您的单元格具有相当复杂的UI,那么单独为单元格创建单独的xib更有意义。然后以编程方式使用表格视图注册xib 。这样,你只有一个单元格的副本,并且在ui发生变化时更好地维护它。

If your cell has a fairly complicated UI, then it makes more sense to create separate xib for the cell alone. Then programmatically register the xib with the table view. That way, you will have only one copy of the cell and much better at maintaining it when there are changes to the ui.

这篇关于我可以在多个tableViews中使用单个原型单元吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆