我有一个UIViewController
实现 TableViews 委托和数据源协议的。现在我想向单元格添加“滑动删除”手势。
我该怎么办。
我给出了一个空白的commitEditingStyle
方法实现,并将 Editing 属性设置为 YES。
仍然没有刷卡功能。
现在我需要单独添加UISwipeGesture
到每个单元格吗?
还是我错过了什么?
我有一个UIViewController
实现 TableViews 委托和数据源协议的。现在我想向单元格添加“滑动删除”手势。
我该怎么办。
我给出了一个空白的commitEditingStyle
方法实现,并将 Editing 属性设置为 YES。
仍然没有刷卡功能。
现在我需要单独添加UISwipeGesture
到每个单元格吗?
还是我错过了什么?
正如Dan上面评论的那样,您需要实现以下 table view 委托方法:
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 的 dataSource 是 UITableViewContoller 的子类时,这不是必需的 - 如果未覆盖此方法,则默认返回 YES。在所有其他情况下,您必须实施它。
编辑:我们一起发现了问题 -如果表未处于编辑模式,则tableView:editingStyleForRowAtIndexPath:
返回。UITableViewCellEditingStyleNone
// 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);
}
Kyr Dunenkoff聊天的结论是
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
}
如果您需要在滑动时显示删除按钮,则不应定义。
如果您使用 anNSFetchedResultsControllerDelegate
来填充表格视图,这对我有用:
tableView:canEditRowAtIndexPath
始终返回 true在您的tableView:commitEditingStyle:forRowAtIndexPath
实现中,不要直接从表视图中删除该行。相反,使用您的托管对象上下文删除它,例如:
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)")
}
}
这对我来说也是一个问题......我只能每 10 次左右尝试刷卡删除才能工作一次。事实证明,gesture
电视上的 父视图控制器中的另一个手势被阻止了。电视嵌套在MMDrawerController
(可滑动的抽屉布局)中。
只需将抽屉控制器中的手势识别器配置为不响应侧面抽屉中的关闭手势,就可以在我的电视中进行滑动删除操作。
您也可以尝试使用以下方法执行以下操作gesture delegate
:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
这是快捷版
// 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
}
}
editing
根据我的经验,似乎您必须UITableView
准备好NO
才能刷卡上班。
self.tableView.editing = NO;
在 iOS 8.0 之后,您可以自定义您的操作
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil];
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger count = [posts count];
if (row < count) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleNone;
}
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger count = [posts count];
if (row < count) {
[posts removeObjectAtIndex:row];
}
}
您可以通过在 XCode 5 中创建 UITableViewController 类(临时)来查看所有必要的方法,然后复制您想要使用的方法。您需要的那些方法将用您想要的预填充行注释掉。