测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
Released最后发布 | 2014年12月 |
由Manfred Scheiner 维护。
完全兼容于 iOS 7 和 iOS 8
通过扩展 Apple 自身的 "swipe to delete"-实现而不是重写它,提供快速删除按钮,以在 UITableView 的 "swipe to delete"-菜单中实现 Mail.app 中的效果,因此不改变 UITableView 的标准行为。
如果您正在使用自定义的 UITableViewCell 子类,那么将其更改为从 MSCMoreOptionTableViewCell 继承而不是 UITableViewCell。如果您直接使用 UITableViewCell,则将其替换为 MSCMoreOptionTableViewCell(详细信息请参阅以下代码段)。然后设置单元格的代理到您的 UITableViewController 并完成设置!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"MSCMoreOptionTableViewCell";
MSCMoreOptionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[MSCMoreOptionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.delegate = self;
}
cell.textLabel.text = @"Cell";
return cell;
}
如果您的项目中使用 Storyboards,请查看包含使用 Storyboard 的示例的演示项目。
如果按下更多按钮,则调用可选的代理方法。
- (void)tableView:(UITableView *)tableView moreOptionButtonPressedInRowAtIndexPath:(NSIndexPath *)indexPath;
通过传递给 UITableViewDataSource 的标准的 -tableView:commitEditingStyle:forRowAtIndexPath:
方法来信号触发的删除按钮。
有多个可选的代理方法可用于自定义“Delete”和“More”按钮。查看 MSCMoreOptionTableViewCellDelegate.h
中的内联文档以获取详细信息。
可以使用 configurationBlock
完全自定义两个按钮。
[cell setConfigureButtonsBlock:^(UIButton *deleteButton, UIButton *moreOptionButton, CGFloat *deleteButtonWitdh, CGFloat *moreOptionButtonWidth) {
// Hide delete button every second row
*deleteButtonWitdh = (indexPath.row - 1) % 2 == 0? 0.f : *deleteButtonWitdh;
// Give the 'More' button a orange background every third row
moreOptionButton.backgroundColor = (indexPath.row - 2) % 3 == 0 ? [UIColor orangeColor] : moreOptionButton.backgroundColor;
// Set a trash icon as 'Delete' button content
[deleteButton setTitle:nil forState:UIControlStateNormal];
[deleteButton setImage:[UIImage imageNamed:@"Trash.png"] forState:UIControlStateNormal];
[deleteButton setImageEdgeInsets:UIEdgeInsetsMake(0.f, 20.f, 0.f, 20.f)];
];
默认情况下,deleteButtonWidth
和 moreOptionButtonWidth
会设置为 MSCMoreOptionTableViewCellButtonWidthSizeToFit
按钮宽度将按 contentSize + edgeInsets
计算。
要隐藏按钮,将其宽度设置为零。
MSCMoreOptionTableViewCell 如许多其他扩展原有功能解决方案一样,依赖于现有供应商代码,因此如果苹果在未来的 iOS 版本中大幅修改其“滑动删除”实现,则可能出现“更多”按钮直到 MSCMoreOptionTableViewCell 被采用才出现的情况。但对于你作为开发者来说,重要的是要知道,由于苹果“滑动删除”实现的变更,MSCMoreOptionTableViewCell 不会破坏您的应用程序或 UITableView 的标准功能。
如 @steipete 所指出,有方法在不使用第三方代码和私有 API 的情况下实现“更多”按钮。这是一个相当简化的方式,但你必须考虑的是,这是未经文档记录的行为,“删除”和“更多”按钮不可定制。
苹果在测试版 2 中添加了一个真正酷的方法,这使得在“滑动删除”菜单中实现自定义按钮比以往任何时候都容易。查看我的 gist 以查看一个工作示例。
MSCMoreOptionTableViewCell 由 Manfred Scheiner (@scheinem - scheinem.com) 创建。当然,还有一份 所有贡献者的完整列表 可用。
MSCMoreOptionTableViewCell 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。若希望在使用时不提及署名,请联系 Manfred Scheiner。