- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 NS_ENUM。我已在 .m 文件中编写了以下代码。现在,当我的 cellForRowAtIndexPath
被调用时。我得到了索引路径。现在对应于该索引路径,我想提取与其关联的字符串。例如,对于索引路径 0,我想提取图像。
typedef NS_ENUM(NSInteger, TABLE_SECTION_ITEMS)
{
Images = 0,
Videos = 1,
Documents = 2,
Audios = 3
};
最佳答案
在这种情况下,我们通常所做的就是始终将枚举中的最后一项保留为“count”或“last”。以您的情况为例:
typedef NS_ENUM(NSInteger, TABLE_SECTION_ITEMS)
{
TABLE_SECTION_Images,
TABLE_SECTION_Videos,
TABLE_SECTION_Documents,
TABLE_SECTION_Audios,
TABLE_SECTION_Count
};
我们不指定值,因为这可能会破坏逻辑。但您可以对项目重新排序,也可以通过将项目放在“count”后面来弃用它们。
它的用法看起来像这样:
@interface ViewController()<UITableViewDelegate, UITableViewDataSource>
@end
@implementation ViewController
- (NSString *)tableSectionName:(TABLE_SECTION_ITEMS)item {
switch (item) {
case TABLE_SECTION_Images: return @"Images";
case TABLE_SECTION_Videos: return @"Videos";
case TABLE_SECTION_Documents: return @"Documents";
case TABLE_SECTION_Audios: return @"Audios";
case TABLE_SECTION_Count: return nil;
}
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return TABLE_SECTION_Count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = [self tableSectionName:indexPath.row];
return cell;
}
@end
因此,行计数只是 TABLE_SECTION_Count
,您可以自然地在 NSInteger
和枚举之间进行转换,如 [self tableSectionName:indexPath.row]
.
当然,仍然需要像 tableSectionName
中那样完成字符串映射。它并没有闲置,但现在更易于管理;
当您添加新的枚举值(例如TABLE_SECTION_Documents2
)时,您将在tableSectionName
中收到n个错误,您需要添加新的案例(或者更确切地说是关联的错误)它表示 Control 可能到达非 void 函数的末尾)。因此,作为开发人员,您被迫填写如下:
- (NSString *)tableSectionName:(TABLE_SECTION_ITEMS)item {
switch (item) {
case TABLE_SECTION_Images: return @"Images";
case TABLE_SECTION_Videos: return @"Videos";
case TABLE_SECTION_Documents: return @"Documents";
case TABLE_SECTION_Documents2: return @"Documents";
case TABLE_SECTION_Audios: return @"Audios";
case TABLE_SECTION_Count: return nil;
}
}
这里的另一个好处是我们可以拥有两次“文档”。要弃用一个项目,我们需要做的就是将枚举移到计数之后:
typedef NS_ENUM(NSInteger, TABLE_SECTION_ITEMS)
{
TABLE_SECTION_Images,
TABLE_SECTION_Videos,
TABLE_SECTION_Documents2,
TABLE_SECTION_Audios,
TABLE_SECTION_Count, // Always keep last
// Deprecated items
TABLE_SECTION_Documents
};
现在,旧的“文档”将不会在您的 TableView 中列出,无需更改任何代码。不理想,但仍然很整洁。
关于ios - 如何使用 NS_ENUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573368/
我想使用 NS_ENUM。我已在 .m 文件中编写了以下代码。现在,当我的 cellForRowAtIndexPath 被调用时。我得到了索引路径。现在对应于该索引路径,我想提取与其关联的字符串。例如
我正在使用 spiffy new NS_ENUM 尝试在我的 objective-c iOS 项目中定义枚举。 我在类的 header 中声明 NS_ENUM,如下所示: NS_ENUM(int, S
我在所有三个 enum 上收到错误 function definition is not allowed here。我最近更新到 Xcode 6.0.1,现在收到该错误。我该如何解决这个错误,我应该避
示例 .h 文件: @interface MyClass : NSObject typedef NS_ENUM(int, myType) { Something, SomethingE
我有一个枚举,它在 constant.h 文件 ( Objective-C ) 中 typedef NS_ENUM (NSInteger, EEFieldType) { EEFieldTypeHi
Objective C 提供了几种声明枚举的方法。它可以通过 typedef enum 或 NS_ENUM 声明。 NS_ENUM 宏以类型名作为参数,我没有完全理解它的意思。我没有在 Apple 官
注意:这不是this question的骗局。 . 我正在尝试使用名为 GBCli 的库在 swift .它是用 Objective-C 编写的。我在使用特定枚举时遇到问题: /** Various
我有一个NS_Enum: typedef NS_ENUM(NSInteger, Months) { January, February, March, April,
我知道以前有人问过这样的问题。我查看了网上的示例,但不知道如何在我的代码中使用 NS_ENUM。我有一个错误代码列表。为了管理它们,我想使用 NS_ENUM。我创建了一个类,其中包含应用程序的所有公共
我遇到了奇怪的行为。我曾经有: @property (nonatomic) ApplicationState applicationState; 直接在我的应用程序类中。现在它被提取到协议(proto
我在 iOS 应用程序中使用 typedef NS_ENUM,但出现此错误:“链接器命令失败,退出代码为 1”。通常这很容易修复,但这次我找不到解决方案... 这是我的 NS_Enum: typede
是否有一种优雅的方法来获取 NS_ENUM 中的项目总数?最大值呢? 一些例子: typedef NS_ENUM(NSInteger, MyEnum) { MyEnumA = 0, M
我在 Xcode5 中使用 clang 预处理了以下代码。 typedef NS_ENUM(NSInteger, MyStyle) { MyStyleDefault, MyStyleC
我创建了一个这样的枚举 typedef NS_ENUM(NSInteger, PermissionStages) { thePermissionNotDetermine = 0, th
我在 EnumsHeader.h 中创建了一个 Objective-C 枚举文件并将其导入 -Bridging-Header.h #import "EnumsHeader.h" .我能够在 Swift
我正在尝试创建一个匹配 typedef enum 和 typedef NS_ENUM 声明的 OCLint 规则,但收效甚微。我有一个 Objective-C 文件 (TestClass.m),其中包
在 Objective-C 中创建具有特定类型的枚举的正确方法是什么? NS_ENUM 和 NS_OPTIONS 是如何工作的? NS_OPTIONS 用于 mask ,例如 NSAutoresizi
我不想强制我的用户更新到 iOS 6,因此我想找到一种方法来使用带有自定义类型和枚举的 NSCoder。 我找到了 this article这解释了最近引入的 NS_ENUM 宏,它基本上使运行时库更
我正在使用 Swift 3 和 xcode 8。我是 objective-C 的新手,也是 swift 的新手,所以这可能是个白痴问题。但是这里开始了! 我正在从 objective-C 过渡到 sw
我有一个 Objective-C 文件,其中的枚举定义如下: typedef NS_ENUM(NSInteger, State) { State_ACTIVE = 0, State_PENDI
我是一名优秀的程序员,十分优秀!