Swift中索引为3的UITableViewEditingStyle

Bog*_*hin 2 uitableview swift

目标是在表视图中的每个单元格上放置单选按钮.我不想为此使用图像.下面的视频课在YouTube上,我发现了无证可能性在Objective-C来做到这一点.

所以我在objc中做到了这一点并且有效.我试图在Swift上重复这个,但我找不到如何使用这种无证的可能性)

细节:

编辑模式用于在左侧添加此图标,如下所示 [self.tableView setEditing:YES animated:YES];

此方法用于为编辑模式设置图标:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 3;
}
Run Code Online (Sandbox Code Playgroud)

但有趣的是,UITableViewCellEditingStyle因为枚举只有3个值.意味着您只能将值返回为0,12

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
    UITableViewCellEditingStyleNone,
    UITableViewCellEditingStyleDelete,
    UITableViewCellEditingStyleInsert
};
Run Code Online (Sandbox Code Playgroud)

但在上面的例子中价值是3有效的.奇迹般有效. 这是obj-c

如何在Swift中执行此操作?如何为枚举返回值3?这是Swift中的枚举示例:

public enum UITableViewCellEditingStyle : Int {
    case None
    case Delete
    case Insert
}
Run Code Online (Sandbox Code Playgroud)

现在我只能从Swift中的枚举中返回预定义的值

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.None
    }
Run Code Online (Sandbox Code Playgroud)

但如果试图返回价值return UITableViewCellEditingStyle(rawValue: 3)!- 它什么都不做.

rob*_*off 5

你不应该试试这个.它是一个私有API,因此Apple可以在未来版本的iOS中随意更改它,从而破坏您的应用程序.试图使用这个私有API是一个坏主意.

这是你如何做到的,如果它不是一个如此可怕的想法,它是:

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    return unsafeBitCast(3, UITableViewCellEditingStyle.self)
}
Run Code Online (Sandbox Code Playgroud)