我有一个 UIViewController
,实现了TableView的delegate和datasource协议。现在我想给单元格添加"滑动删除"手势。
我该如何做呢?
我已经提供了commitEditingStyle
方法的空白实现,并将Editing属性设置为YES。
但仍然没有出现滑动功能。
现在我需要单独为每个单元格添加UISwipeGesture
吗?
还是我漏掉了什么?
我有一个 UIViewController
,实现了TableView的delegate和datasource协议。现在我想给单元格添加"滑动删除"手势。
我该如何做呢?
我已经提供了commitEditingStyle
方法的空白实现,并将Editing属性设置为YES。
但仍然没有出现滑动功能。
现在我需要单独为每个单元格添加UISwipeGesture
吗?
还是我漏掉了什么?
正如Dan上面所评论的,您需要实现以下表视图委托方法:
tableView:canEditRowAtIndexPath:
tableView:commitEditingStyle:forRowAtIndexPath:
注意:我已经在iOS 6和iOS 7中尝试过此方法。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES - we will be able to delete all rows
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform the real delete action here. Note: you may need to check editing style
// if you do not perform delete only.
NSLog(@"Deleted row.");
}
如果你需要在单元格滑动时显示删除按钮,就不必设置 editing:YES
。相反,你需要实现tableView:canEditRowAtIndexPath:
方法,并从其中返回想要编辑/删除的行的YES。当tableView的数据源是UITableViewContoller的子类时,这不是必需的 - 如果未覆盖此方法,则默认情况下它会返回YES。对于其他所有情况,你都需要实现它。
编辑:我们一起找到了问题所在 - 如果表格不处于编辑模式,tableView:editingStyleForRowAtIndexPath:
会返回UITableViewCellEditingStyleNone
。
tableView:canEditRowAtIndexPath:
可能不够。您还需要实现 tableView:commitEditingStyle:forRowAtIndexPath
。 - dwlz- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- Hugo// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not 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) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// let the controller to know that able to edit tableView's row
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code)
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
// add the action button you want to show when swiping on tableView's cell , in this case add the delete button.
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in
// Your delete code here.....
.........
.........
})
// You can set its properties like normal button
deleteAction.backgroundColor = UIColor.redColor()
return [deleteAction]
}
// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return(YES);
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
}
NSFetchedResultsControllerDelegate
来填充表视图,这对我有效:
tableView:canEditRowAtIndexPath
returns true alwaysIn your tableView:commitEditingStyle:forRowAtIndexPath
implementation, do not delete the row directly from the table view. Instead, delete it using your managed object context, e.g.:
if editingStyle == UITableViewCellEditingStyle.Delete {
let word = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Word
self.managedObjectContext.deleteObject(word)
self.saveManagedObjectContext()
}
func saveManagedObjectContext() {
do {
try self.managedObjectContext.save()
} catch {
let saveError = error as NSError
print("\(saveError), \(saveError.userInfo)")
}
}
这是 Swift 版本
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
UITableView
的editing
设置为NO
才能使滑动生效。
self.tableView.editing = NO;
gesture
被父视图控制器中的另一个手势阻塞了。电视嵌套在一个MMDrawerController
(可滑动抽屉布局)中。gesture delegate
进行类似的操作:-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}