【问题标题】:Stange animation when deleting UITableViewCell删除 UITableViewCell 时的 Stange 动画
【发布时间】:2011-11-02 05:37:22
【问题描述】:

我正在使用此代码删除一个 UITableViewCell,但是当我滑动删除时,它会先显示右侧的减号,然后再删除。

我拍了一段视频来帮助说明我的观点:Video on YouTube

- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
    [self.tableView setEditing: !self.tableView.editing animated:YES];

    if (self.tableView.editing)
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
    else
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        PFObject *routine= [self.routineArray objectAtIndex:indexPath.row];
        [routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
                [self.routineArray removeObjectAtIndex:indexPath.row];
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                //         [MKInfoPanel showPanelInView:self.view type:MKInfoPanelTypeError title:@"Routine Deleted" subtitle:@"You have succesfully deleted the routine!" hideAfter:2];
            } else {
                NSLog(@"%@", error);
            }
        }];   
    }
}

编辑:

- (void)loadData
{
    PFQuery *query = [PFQuery queryWithClassName:@"Routine"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.routineArray = [objects mutableCopy];
            [self.tableView reloadData];
        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];   
}

-(void)addRoutine
{   
    PFObject *routine = [[PFObject alloc] initWithClassName:@"Routine"];
    [routine setObject:self.entered forKey:@"name"]; 
    [routine saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            [self loadData];
        } else {
            // There was an error saving the routine.
        }
    }];   
}

【问题讨论】:

  • 当您尝试不同类型的动画并且不褪色时会发生什么?还有,哇,你的演示视频里的音乐真是太疯狂了。
  • 我将动画设置为 UIAnimation none 样式,但问题仍然存在。

标签: iphone objective-c cocoa-touch uitableview


【解决方案1】:

看起来有两个问题。第一个看起来像 -deleteInBackgroundWithBlock: 在按下删除按钮后需要相当长的时间来执行它的块。如果您不使用NSFetchedResultsController

,则可以在从核心数据存储中删除数据之前尝试删除 dataSource 对象和 tableView 行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        PFObject *routine = [self.routineArray objectAtIndex:indexPath.row];
        [self.routineArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
                //[MKInfoPanel showPanelInView:self.view type:MKInfoPanelTypeError title:@"Routine Deleted" subtitle:@"You have succesfully deleted the routine!" hideAfter:2];
            } else {
                NSLog(@"%@", error);
            }
        }];   
    }
}

如果您不喜欢淡出该行的不透明度,也可以使用其他动画。如果您只针对 iOS 5.0,您可以使用 UITableViewRowAnimationAutomatic 让 UIKit 尝试在特定情况下选择最好看的动画。

另一个问题似乎是在按下删除后重新打开了编辑模式。您不需要覆盖-setEditing:animated:,因此请尝试完全删除该方法。 在您的-viewDidLoad: 中,您可以执行以下操作以免费获得编辑行为:

self.navigationItem.leftBarButtonItem = self.editButtonItem;

见:

还需要注意的是,在查看编辑状态时,应该使用isEditing访问器。

为避免调用-reloadData,您只需将单个新对象添加到数据源数组中,然后添加 tableView 行,然后将其保存到核心数据存储中。这与从表视图中删除一行时必须做的相反。如果您需要将 routine 对象附加到 tableView 的末尾并且没有自定义排序顺序,这将起作用。否则,您必须将routine 对象插入self.routineArray 中所需的索引处,然后创建正确的indexPath 以将tableView 行插入到tableView 中的所需位置。

- (void)addRoutine
{   
    PFObject *routine = [[PFObject alloc] initWithClassName:@"Routine"];
    [routine setObject:self.entered forKey:@"name"]; 
    [self.routineArray addObject:routine];
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([self.routineArray count]-1) inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath withRowAnimation:UITableViewRowAnimationAutomatic
    [routine saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            [self loadData];
        } else {
            // There was an error saving the routine.
        }
    }];   
}

【讨论】:

  • 谢谢,效果很好。你能帮我做动画吗?现在我只是在添加单元格时重新加载表格。但是我该如何设置它来代替像UITableViewRowAnimationAutomatic 这样的特殊动画之一呢?我在问题中添加代码。
猜你喜欢
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多