简体   繁体   English

这是滥用还是滥用typedef枚举(与UITableView一起使用静态内容)

[英]Is this an abuse or misuse of typedef enum (using with UITableView with static content)

Sometimes I need to make a UITableView that resembles a menu and has rows that are known before compiling. 有时候我需要创建一个类似菜单的UITableView ,并且在编译之前有一些已知的行。 I typically do this: 我通常这样做:

typedef enum {
    PEPSI_SODA,
    COKE_SODA,
    DRPEPPER_SODA,
    DIET_SODA,
    COUNT_SODA
} SODAS;

I then return COUNT_SODA for the numberOfRowsInSection and a switch (indexPath.row) { case PEPSI_SODA: cell.textLabel.text = @"Pepsi" break; } 然后我返回COUNT_SODAnumberOfRowsInSection和一个switch (indexPath.row) { case PEPSI_SODA: cell.textLabel.text = @"Pepsi" break; } switch (indexPath.row) { case PEPSI_SODA: cell.textLabel.text = @"Pepsi" break; } for the cellForRowAtIndexPath method. switch (indexPath.row) { case PEPSI_SODA: cell.textLabel.text = @"Pepsi" break; }cellForRowAtIndexPath方法。 Is this a "proper" use of typedef enum or is there a better way to approach these types of situations? 这是对typedef enum的“正确”使用还是有更好的方法来处理这些类型的情况?

It should safely work. 它应该安全地工作。 However, I would prefer the use of dictionaries, there you could bind the data together and you would be more flixible. 但是,我更喜欢使用字典,在那里你可以将数据绑定在一起,你会更加灵活。

Create some type of UITableViewController subclass that you can pass a dictionary on the constructor. 创建一些类型的UITableViewController子类,您可以在构造函数上传递字典。 That way you can always reuse the same class in your projects. 这样,您始终可以在项目中重用相同的类。 Your method is not reusable and produces a lot of effort to extend since you have to change things in various places to add a row. 您的方法不可重用,并且需要进行大量的扩展,因为您必须在各个位置更改内容以添加行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM