Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analyze the elegant code structure under iOS plural cell

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "analyzing the elegant code structure under iOS plural cell". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "analyzing the elegant code structure under iOS plural cell".

Demand

Multiple cell may appear on one page.

This requirement should be very common, and the problem that needs to be solved is how to enable multiple cell to respond to the same method together, so that the outside does not need to know the specific type of cell, just call the same method for configuration.

After asking my friends, we are basically two factions.

Protocol base class

Personally, I used to use protocols to constrain multiple cell, by making cell follow the same protocol and implementing the protocol method, so that the outside can achieve the effect of unified configuration.

/ / cell jointly follows this protocol @ protocol ModuleACellConfigPropotol-(void) configCellWithModel: (KTModel *) model;@end calls the method UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:cellID] through the protocol; if ([cell respondsToSelector:@selector (configCellWithModel:)]) {[cell configCellWithModel:model];}

When it comes to base class inheritance, it is generally disgusting and ready for refactoring, so we don't think about it.

Coupling

In the standard case of MVC, the configuration method of cell should look like this:

Interface KTTableViewCell00: UITableViewCell- (void) configShowViewWithTitle00: (NSString *) title;@end@interface KTTableViewCell01: UITableViewCell- (void) configShowViewWithTitle01: (NSString *) title;@end

External assignment should not pass model to cell, but only pass parameters specified by cell

[cell configShowViewWithTitle01:model.title]

The protocol, in order to achieve a unified configuration, must be constrained by the same method. However, the cell actually have different sufficient and necessary parameters, so they can only pass the whole model as a parameter.

@ protocol ModuleACellConfigPropotol-(void) configCellWithModel: (KTModel *) model;@end

Decoupling

Through the way of protocol constraints, the unified configuration has been successfully realized.

However, there is a problem, so that cell and model have a coupling, so that cell can not be reused.

In terms of the results, this is not perfect.

To solve this problem, I think it's a good idea to add another layer of adapter between cell and the protocol.

And this adapter, I used Category to implement.

@ interface KTTableViewCell00 (ModuleA) @ end@implementation KTTableViewCell00 (ModuleA)-(void) configCellWithModel: (KTModel *) model {[self configShowViewWithTitle00:model.title];} @ end

Finally, it is called:

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {KTModel * model = self.dataArr [indexPath.row]; NSString * cellID = model.identifier; UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:cellID]; if ([cell respondsToSelector:@selector (configCellWithModel:)]) {[cell configCellWithModel:model];} return cell;}

At this point, I believe you have a deeper understanding of "analyzing the elegant code structure under the iOS plural cell". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report