UItableview滑动删除iOS 11不刷新/重新加载数据
问题描述:
我是编程新手。下面的代码使用滑动删除一行,但在刷新所有行列表后出现。 我有以下代码:UItableview滑动删除iOS 11不刷新/重新加载数据
- (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;
}
答
在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;
}
作为行从表中删除视觉这将会从您的数据源中删除的对象。
它似乎没有从任何数据源中删除该行。 在您正在记录删除的索引路径的completionHandler中,您需要从数据源(数组等)中删除该行。否则,当你的表从数据源重新加载时,它会重新出现。 – Stephen
谢谢,最好的办法是做什么? – Simi
你用什么结构作为数据源?你能分享你的cellForRowAtIndexPath的一些代码吗? – Stephen