我如何得到删除按钮显示时,在一个UITableViewCell滑动?该事件永远不会引发,删除按钮也永远不会出现。
当前回答
斯威夫特4
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "delete") { (action, indexPath) in
// delete item at indexPath
tableView.deleteRows(at: [indexPath], with: .fade)
}
return [delete]
}
其他回答
SWIFT 5:适用于iOS 13+
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (contextualAction, view, boolValue) in
//Code I want to do here
}
let editAction = UIContextualAction(style: .destructive, title: "Edit") { (contextualAction, view, boolValue) in
//Code I want to do here
}
let swipeActions = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
return swipeActions
}
如果你采用可变数据源,你必须将委托回调移动到UITableViewDiffableDataSource子类。例如:
class DataSource: UITableViewDiffableDataSource<SectionType, ItemType> {
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if let identifierToDelete = itemIdentifier(for: indexPath) {
var snapshot = self.snapshot()
snapshot.deleteItems([identifierToDelete])
apply(snapshot)
}
}
}
}
在启动期间(-viewDidLoad或在storyboard)做:
self.tableView.allowsMultipleSelectionDuringEditing = false
重写以支持表视图的条件编辑。只有当你要为某些项返回NO时,才需要执行这个操作。默认情况下,所有项目都是可编辑的。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
我有一个问题,我刚刚设法解决了,所以我分享它,因为它可能会帮助别人。
我有一个UITableView,并添加了显示的方法来启用滑动删除:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
我正在进行更新,使我可以将表设置为编辑模式并启用多选。为此,我添加了来自苹果TableMultiSelect示例的代码。当我开始工作时,我发现我的滑动删除功能已经停止工作。
事实证明,添加以下行到viewDidLoad是问题所在:
self.tableView.allowsMultipleSelectionDuringEditing = YES;
有了这一行,多选可以工作,但滑动删除就不行了。没有这条线,情况正好相反。
解决办法:
添加以下方法到你的viewController:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
self.tableView.allowsMultipleSelectionDuringEditing = editing;
[super setEditing:editing animated:animated];
}
然后在你的方法中,将表格进入编辑模式(例如,从一个按钮按下),你应该使用:
[self setEditing:YES animated:YES];
而不是:
[self.tableView setEditing:YES animated:YES];
这意味着只有在表处于编辑模式时才启用多选功能。
同样,这也可以通过以下方法在SWIFT中实现
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete){
testArray.removeAtIndex(indexPath.row)
goalsTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
推荐文章
- 以编程方式创建segue
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- 新的自动引用计数机制是如何工作的?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?