【问题标题】:Defining enum at runtime to populate UITableView在运行时定义枚举以填充 UITableView
【发布时间】:2013-11-04 20:54:04
【问题描述】:

我正在使用枚举填充 UITableView。现在在运行时,在获得服务器数据后,我需要决定是否填充第一部分。因此,我需要删除第一个枚举 (MyListSectionType1) 条目以使其正常工作。

typedef enum {
    MyListSectionType1,
    MyListSectionType2,
    MyListSectionType3,
    MyListSectionType4,
    MyListSectionType5,
    MyListSectionType6,
    MyListSectionType7,

    MyListSectionTypeMax,

} MyListSectionType;

我想过尝试使用下面的代码,但如何在运行时定义 showShow 是另一个问题。我尝试在获取服务器数据的类中定义它,但这不起作用。有什么线索吗?

typedef enum {
#ifdef showShow
    MyListSectionType1,
#endif
    MyListSectionType2,
    MyListSectionType3,
    MyListSectionType4,
    MyListSectionType5,
    MyListSectionType6,
    MyListSectionType7,

    MyListSectionTypeMax,

} MyListSectionType;

【问题讨论】:

  • 您不能在运行时更改enum。事实上,在运行时,enum 并不存在。这一切都只是在编译时转换为整数。你为什么不改变你的问题,让它涵盖你试图解决的问题,而不是询问一种可能的解决方案。
  • 无论如何你都不能用#ifdef 来做,因为这是一个预处理器指令。在编译时评估。

标签: ios objective-c cocoa-touch uitableview enums


【解决方案1】:

您无法在运行时执行此操作。 #ifdef 也是一个预处理器指令,它在编译期间进行处理。 您可以改用数组并在运行时添加和删除项目。所以你会有类似

NSMutableArray *arr = @[@"Section 1", @"Section 2", @"Section 3"];

如果您得到不需要第一部分的回复,那么您可以致电

[arr removeObjectAtIndex:0];

【讨论】:

  • 只有第一个元素的索引为 0 :-)
【解决方案2】:

好的。我已经通过将 MyListSectionType1 移动到枚举列表的最后,然后在 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 有条件地返回 MyListSectionTypeMax - 1 来解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多