关于静态细胞与动态细胞的混淆 [英] confusion about static cells vs dynamic cells

查看:169
本文介绍了关于静态细胞与动态细胞的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出除了动态单元格之外如何有一些静态单元格(我认为)但是当我启动一个新的Master-Detail应用程序并将默认的动态原型切换到静态单元格时,它会崩溃。 / p>

我认为,因为我还是新手,我不明白所有组件(表视图,单元格,代理)是如何连接的。在我切换到静态细胞后,我制作了三个细胞,但它们没有显示出来。该应用程序崩溃。



我可以包含代码但基本上,我创建一个新的Master-Detail,切换到静态单元格,更改三个单元格的名称然后崩溃(在主循环中。)



先谢谢你。

解决方案

如果我理解你的问题,这就是你要找的解释:



原型(可重用性):



使用 动态细胞 时,使用 原型细胞 。这意味着在IB或您的代码中,您创建一个 UITableViewCell 的实例,并为其指定一个标识符( reuseIdentifier )。 UITableView 使用此 原型 生成所需数量的单元格(行)。 UITableView 为填充屏幕所需的单元格创建(并使用内存)。一旦其中一些单元格离开屏幕, UITableView 重新使用 - 回收 他们。



您可以在<$中定义 一个 原型单元格C $ C>的UITableView 。我们的想法是每个原型都有不同的用途。例如,您定义一个只有一个大 UILabel 的原型单元格,其目的是使用静态文本。您定义另一个只有一个 UIImageView 的原型。这取决于您的设计以及您希望如何显示数据。



要使用原型单元格,请在IB中使用 动态原型单元格

em> ,并设置标识符。然后你必须实现 UITableViewDataSource 方法,例如:


   -  tableView:numberOfRowsInSection:
- tableView:cellForRowAtIndexPath:


这些方法是UITableView的委托方法。



如果不使用IB,你将在中创建单元格 - tableView:cellForRowAtIndexPath: 方法:


UITableViewCell * cell = [[UITableViewCell alloc]
initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier];


静态:



静态单元的概念只是简单性。您可以使用原型单元格(例如,只有一个原型)并输入静态 NSArray ,例如,作为数据源。为了完成实现 UITableViewDataSource 方法的任务,Apple引入了静态单元格,您只能使用故事板和IB。在IB中,您选择表格视图,然后选择 静态单元格 ,然后键入文本或设置图像。
您不希望以后在应用程序运行时对静态单元格进行任何动态更改,因为它并不意味着,您将不得不实现更多不值得的方法。虽然获取所选行的 indexPath 并不是很重要,但是如果您需要从选择静态单元格中推送新视图,那么使用segues,而不是实现任何代码。



使用静态单元格时,不应实现 UITableViewDataSource 方法,否则你的应用程序崩溃,反之亦然,如果你在使用原型单元格时没有实现 UITableViewDataSource 方法(必需的),你的应用程序会崩溃。


I'm trying to figure out how to have a few static cells in addition to dynamic cells (I think) but when I start a new Master-Detail app and switch the default Dynamic Prototypes to Static Cells, it crashes.

I think, since I'm still new at this, that I don't understand how all of the components (table views, cells, delegates) are wired. After I switched to Static Cells I made three cells but they don't show up. The app just crashes.

I can include code but basically, I create a new Master-Detail, switch to Static Cells, change the name of three cells and it crashes (in the main loop).

Thanks ahead for everything.

解决方案

If I understand your question, this is the explanation you are looking for:

Prototype (reusability):

When you use Dynamic cells, you use Prototype cells. That means either in IB or in your code, you create an instance of UITableViewCell, and give it an identifier (reuseIdentifier). The UITableView uses this prototype to generate as many cells (rows) as you need. UITableView create (and use memory) for as many cells as it needs to fill the screen. Once some of these cells go off the screen, UITableView reuses them -- recycles them.

You can define more than one prototype cell in a UITableView. The idea is that each prototype serves a different purpose. For example, you define a prototype cell that has only one big UILabel and its purpose is to use static text. You define another prototype that has only one UIImageView. It depends on your design and how you want to display your data.

To use prototype cells, in IB, you use Dynamic Prototypes cells, and set an identifier. Then you have to implement UITableViewDataSource methods, such as:

– tableView:numberOfRowsInSection: 
– tableView:cellForRowAtIndexPath:

These methods are delegate methods of UITableView.

If not using IB, you would create cells in – tableView:cellForRowAtIndexPath: method:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier];

Static:

The idea of static cells is just simplicity. You can use the prototype cells (e.g. only one prototype) and feed in a static NSArray, for example, as data source. To take the task of implementing UITableViewDataSource methods, Apple introduced static cells, where you would only use the storyboard and IB. In IB, you select the table view, and choose Static Cells instead, and type in your text, or set the image. You don't want to make any dynamic changes to static cells later when the app is running because it is not meant to, and you would have to implement many more methods that it is not worth it. Although it not much of a work to get the indexPath of the selected row, but the idea is if you need to push a new view from selection of a static cell, you would use segues, instead of implementing any code.

When using static cells, you should not implement UITableViewDataSource method, otherwise your app crashes, and vice versa, if you do not implement UITableViewDataSource methods (required ones) when using prototype cells, your app crashes.

这篇关于关于静态细胞与动态细胞的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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