简体   繁体   English

目标C-滑动以删除行

[英]Objective C - Swipe to delete row

I'm new to objective-c and gotten some legacy code for adding new features. 我是Objective-c的新手,并获得了一些用于添加新功能的旧代码。 My app needs that rows from a table are deleted by swiping then out . 我的应用程序需要通过滑动然后将其删除来删除表中的行 The implementation already support the ordering of rows . 实现已经支持行的排序

I have already read previous questions UITableViewCell, show delete button on swipe and using swipe gesture to delete row on tableview . 我已经阅读过前面的问题UITableViewCell,在滑动上显示删除按钮,使用滑动手势在tableview上删除行 I don't want to show any button for deleting rows ( nor the (-) (red left button) neither the DELETE button that is displayed on the right when clicking the (-) red left button ). 我不想显示任何用于删除行的按钮 (也不想显示 (-)(左红色按钮),也不希望单击(-)红色左按钮时显示在右边的DELETE按钮)。

I have already defined the following methods: 我已经定义了以下方法:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"editingStyleForRowAtIndexPath");
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"canEditRowAtIndexPath");
    return YES; // allow that row to swipe
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"commitEditingStyle");
}

The behavior is that with this "configuration" the delete left red button is displayed . 行为是通过此“配置”显示删除左红色按钮 When it is clicked, it displays a DELETE button on the right of the row . 单击后,将在该行的右侧显示一个DELETE按钮 Clicking on it run the commitEditingStyle delegate . 单击它,运行commitEditingStyle委托

Removing the tableView:editingStyleForRowAtIndexPath make disappear the left red button and swipe does not work. 删除tableView:editingStyleForRowAtIndexPath使左红色按钮消失,并且滑动不起作用。 Nor I manage to get execution in the commitEditingStyle delegate. 我也无法在commitEditingStyle委托中获得执行。

Any suggestion or ideas why the commitEditingStyle delegate is not invoked? 关于为什么不调用commitEditingStyle委托的任何建议或想法?

Just write code for deleting row in to - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 只需编写用于删除行的代码即可- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

I have checked and this works fine for me. 我已经检查过了,这对我来说很好。

This will delete row on swipe without showing delete button. 这将在滑动时删除行而不显示删除按钮。 I hope this will solve your problem. 我希望这能解决您的问题。

replace 更换

return UITableViewCellEditingStyleNone 

with

return UITableViewCellEditingStyleDelete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM