作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 .h 文件用于定义我所有的 enum
列表,我现在想使用其中一个列表来填充我拥有的 UITableView
在 UITableViewController
类中创建。但是我不确定如何将 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/
我是一名优秀的程序员,十分优秀!