删除UITableViewCell时的Stange动画 [英] Stange animation when deleting UITableViewCell

查看:59
本文介绍了删除UITableViewCell时的Stange动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我拍了一段视频来说明我的观点: YouTube上的视频

 -(void)setEditing:(BOOL)editing动画:(BOOL)animate 
{
[self.tableView setEditing :!self.tableView.editing动画:是];

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];
[例行deleteInBackgroundWithBlock:^(BOOL成功,NSError *错误){
if(!error){
[self.routineArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// // [MKInfoPanel showPanelInView:self.view类型:MKInfoPanelType错误标题:@例程已删除子标题:@您已成功删除了例程! hideAfter:2];
} else {
NSLog(@%@,error);
}
}];
}
}

编辑:

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

-(void)addRoutine
{
PFObject * routine = [[PFObject alloc] initWithClassName:@ Routine];
[例行setObject:self.entered forKey:@ name];
[例行saveInBackgroundWithBlock:^(BOOL成功,NSError *错误){
if(!error){
[self loadData];
} else {
//保存例程时出错。
}
}];
}


解决方案

好像有两个问题。首先,它看起来像-deleteInBackgroundWithBlock:在按下删除按钮后,要花费大量的时间来执行其块。如果您未使用 NSFetchedResultsController


$ b,则可以尝试从核心数据存储中删除数据之前删除dataSource对象和tableView行。 $ b

 -(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];
[例行deleteInBackgroundWithBlock:^(BOOL成功,NSError *错误){
if(!error){
// [MKInfoPanel showPanelInView:self.view type:MKInfoPanelTypeError标题:@常规删除字幕:@您已成功删除例程! hideAfter:2];
} else {
NSLog(@%@,error);
}
}];
}
}

如果您喜欢某些东西,也可以使用其他动画除了淡化行的不透明度。如果仅针对iOS 5.0,则可以使用 UITableViewRowAnimationAutomatic 让UIKit在特定情况下尝试选择外观最好的动画。



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

  self.navigationItem.leftBarButtonItem = self.editButtonItem; 

请参阅:





还应注意,在检查编辑状态时,应使用isEditing访问器。



<为了避免调用 -reloadData ,您只需将单个新对象添加到dataSource数组中,然后添加tableView行,然后将其保存到核心数据存储中。这与从表视图中删除行时要做的相反。如果您需要将例程对象附加到tableView的末尾并且没有自定义排序顺序,则此方法有效。否则,必须在所需索引处将 routine 对象插入 self.routineArray 中,然后创建适当的indexPath进行插入

 -(void)addRoutine 
{
PFObject *例程= [[PFObject alloc] initWithClassName:@例程];
[例行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
[例程saveInBackgroundWithBlock:^(BOOL成功,NSError *错误){
if(!error){
[self loadData];
} else {
//保存例程时出错。
}
}];
}


I'm using this code to delete a UITableViewCell, but when I swipe to delete, it is showing the minus sign on the right first, then deleting.

I took a video to help illustrate my point: 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);
            }
        }];   
    }
}

Edit:

- (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.
        }
    }];   
}

解决方案

It looks like there are two issues. The first it looks like -deleteInBackgroundWithBlock: is taking a noticeable amount of time to execute it's block after the delete button is pressed. You can try deleting the dataSource object and tableView row before deleting the data from the core data store if you aren't using a NSFetchedResultsController

- (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);
            }
        }];   
    }
}

You can also use a different animation if you prefer something other than fading out the opacity of the row. If you are targeting iOS 5.0 only, you can use UITableViewRowAnimationAutomatic to have UIKit attempt to choose the best looking animation given the circumstances.

The other issue looks like editing mode is turned back on after delete is pressed. You shouldn't need to override -setEditing:animated: so try removing that method completely. In your -viewDidLoad: you can do the following to get editing behavior for free:

self.navigationItem.leftBarButtonItem = self.editButtonItem;

See:

It should also be noted that when you are checking the editing status, you should use the isEditing accessor.

To avoid calling -reloadData, you just add the single new object to your dataSource array, then add a tableView row, then save it to the core data store. This is simply the opposite of what you had to do when deleting a row from the table view. This will work if you need to append the routine object to the end of the tableView and there is no custom sort order. Otherwise, you must insert the routine object into self.routineArray at the desired index and then create the proper indexPath to insert the tableView row at the desired location within the 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.
        }
    }];   
}

这篇关于删除UITableViewCell时的Stange动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆