【问题标题】:How to use enum list to populate UITableView?如何使用枚举列表填充 UITableView?
【发布时间】:2014-05-20 04:14:44
【问题描述】:

我有一个 .h 文件用于定义我的所有 enum 列表,我现在想使用其中一个列表来填充我在 UITableViewController 类中创建的 UITableView。但是我不确定如何将enum 列表读入UITableViewController 类?

这就是我的enum 列表的样子。

// NameNum
typedef enum {John = 0, Jerry = 1, Jack = 2, Jeff = 3, Jonny = 4} Namenum;

我想在UITableView 中显示名称。

更新: 我使用枚举的原因是因为选择了来自UITableView的名称时,我需要在请求中向服务器调用的请求发送相应的号码。

【问题讨论】:

  • 您想在表格视图中显示什么?每个枚举值的整数值?
  • 创建像Namenum obj这样的枚举对象;然后 .m 文件 obj = John 或其他。您可以使用if(obj == John/0) 进行比较
  • @maddy 我想显示名字而不是数字......现在我想我把这些东西放在错误的地方:P
  • @HurkNburkS:实际上 .h 我的意思是全局标题。它会起作用的。

标签: ios objective-c uitableview enums


【解决方案1】:

实际上.. 我有一些不同的方法来实现这一点。您可以直接使用字典而不是枚举。这就是你可以做到的方式

在你的.h中你可以做到这一点..

#define getDictionaryarray() @[                         \
                                @{                      \
                                    @"Name": @"John",   \
                                    @"ID":@"0"          \
                                },                      \
                                @{                      \
                                    @"Name": @"Jerry",  \
                                    @"ID":@"1"          \
                                    },                  \
                                @{                      \
                                    @"Name": @"Jack",   \
                                    @"ID":@"2"          \
                                },                      \
                                @{                      \
                                    @"Name": @"Jeff",   \
                                    @"ID":@"3"          \
                                },                      \
                                @{                      \
                                    @"Name": @"Jonny",  \
                                    @"ID":@"4"          \
                                },                      \
                            ];

然后是表格视图数据源方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = getDictionaryarray();
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    NSArray *dictionaryarray = getDictionaryarray();
    cell.textLabel.text = [[dictionaryarray objectAtIndex:indexPath.row] valueForKey:@"Name"];

这会给您带来一些好处,例如 id 不必采用 0、1、2、3 的形式,它可以是任何数字。您可以向字典中添加更多属性,例如姓氏等。最重要的是,它可以完成您想做的事情:)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = getDictionaryarray();
    NSLog(@"Id for selected Name %@ is %@",[[array objectAtIndex:indexPath.row] valueForKey:@"Name"],[[array objectAtIndex:indexPath.row] valueForKey:@"ID"]);
}

希望对你有帮助……

【讨论】:

    【解决方案2】:

    我会以面向对象的方式处理这个问题,并定义类方法来提供您需要的功能 -

    +(NSString *) stringForName:(Namenum)name
    {
      NSString *ret;
      switch (name)
      {
        case Jack:
            ret=@"Jack";
            break;
        case Jonny:
            ret=@"Johnny";
            break;
        ...
      }
    
      return ret;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 2015-12-04
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多