confusion about static cells vs dynamic cells

前端 未结 1 1750

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 Prototype

相关标签:
1条回答
  • 2021-02-02 14:42

    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.

    0 讨论(0)
提交回复
热议问题