gpt4 book ai didi

ios - 如何使用枚举列表填充 UITableView?

转载 作者:行者123 更新时间:2023-11-28 22:06:55 25 4
gpt4 key购买 nike

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

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

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

我想在 UITableView 中显示名字。

更新:我使用枚举的原因是因为当从 UITableView 中选择一个名称时,我需要在请求中将对应的数字发送到我正在调用的服务器。

最佳答案

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

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

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

然后是 TableView 数据源方法

-(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"]);
}

希望这有助于...

关于ios - 如何使用枚举列表填充 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com