我对编程很陌生。下面的代码与swipe一起工作,删除一行,但在刷新后,所有行列表将返回。我有以下代码:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
title:@"DELETE"
handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
NSLog(@"index path of delete: %@", indexPath);
completionHandler(YES);
}];
delete.backgroundColor = [UIColor purpleColor]; //arbitrary color
UISwipeActionsConfiguration *swipeActionConfig = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
swipeActionConfig.performsFirstActionWithFullSwipe = NO;
return swipeActionConfig;
}我使用的方法如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FCell = (favouriteCell *)[tableView dequeueReusableCellWithIdentifier:@"favouriteCell"];
FCell.selectionStyle=UITableViewCellSelectionStyleNone;
if (FCell == nil)
{
FCell = [[[NSBundle mainBundle]loadNibNamed:@"favouriteCell" owner:nil options:nil] objectAtIndex:0];
}
FCell.nameLBL.text=[[favDetails objectAtIndex:indexPath.row] valueForKey:@"name"];
FCell.poetLBL.text=[[favDetails objectAtIndex:indexPath.row] valueForKey:@"poet"];
return FCell;
}发布于 2017-09-28 13:01:15
在记录删除索引路径的completionHandler中,需要从数据源(数组等)中删除该行。否则,当您的表从数据源重新加载时,它将重新出现。
您的trailingSwipeActionsConfigurationForRowAtIndexPath代码应该如下所示:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
title:@"DELETE"
handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
NSLog(@"index path of delete: %@", indexPath);
[favDetails removeObjectAtIndex:indexPath.row];
completionHandler(YES);
}];
delete.backgroundColor = [UIColor purpleColor]; //arbitrary color
UISwipeActionsConfiguration *swipeActionConfig = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
swipeActionConfig.performsFirstActionWithFullSwipe = NO;
return swipeActionConfig;
}这将从数据源中删除对象,因为从表中直观地删除了行。
https://stackoverflow.com/questions/46371146
复制相似问题