Cocoa touch 为什么在离开编辑模式时,我的UITableView没有对所有行设置相同的动画?

Cocoa touch 为什么在离开编辑模式时,我的UITableView没有对所有行设置相同的动画?,cocoa-touch,uitableview,Cocoa Touch,Uitableview,这与我之前的问题类似,但我之前的问题实际上得到了某种程度的回答,这是它的一种延伸 我有一个UITableView,我正在将其放入和退出编辑模式。在编辑模式下,我会显示一个带有插入图标的附加行。“我的数据源”中的所有其他行都显示一个删除图标 出于某种原因,在离开编辑模式时,数据源中的最后一行的动画设置与所有其他行的动画设置不同。基于行为,这对我来说似乎是一些明显的代码问题,但我似乎无法修复它。我制作了一个动画GIF,向您展示我所说的内容: 有人知道为什么会这样吗?它总是我的数据源的最后一行。自定

这与我之前的问题类似,但我之前的问题实际上得到了某种程度的回答,这是它的一种延伸

我有一个
UITableView
,我正在将其放入和退出编辑模式。在编辑模式下,我会显示一个带有插入图标的附加行。“我的数据源”中的所有其他行都显示一个删除图标

出于某种原因,在离开编辑模式时,数据源中的最后一行的动画设置与所有其他行的动画设置不同。基于行为,这对我来说似乎是一些明显的代码问题,但我似乎无法修复它。我制作了一个动画GIF,向您展示我所说的内容:

有人知道为什么会这样吗?它总是我的数据源的最后一行。自定义插入行图标的动画效果很好。这不是一大堆代码,但一次查看所有代码最容易,因此我将把您链接到Github上的确切代码:


问题似乎不在于tableView或与之相关的任何代码。这似乎是由于拉刷新视图导致的。不幸的是,我没有该组件的代码。我尝试了你的拉刷新,它是完美的工作。因此,请删除拉刷新,看看发生了什么

#导入
#import <UIKit/UIKit.h>

@interface zViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *table;
@end



#import "zViewController.h"

@interface zViewController ()
@property (nonatomic, retain) NSArray* data;
@end

@implementation zViewController{
    BOOL _editing;
}
@synthesize data = _data;
@synthesize table = _table;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _editing = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editButtonWasPressed:)];
    [self loadRecords];
}

- (void)editButtonWasPressed:(id)sender {
    self.navigationItem.leftBarButtonItem  = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(saveButtonWasPressed:)];
    [self setEditing:YES animated:YES];
}

- (void)saveButtonWasPressed:(id)sender {
    self.navigationItem.leftBarButtonItem  = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editButtonWasPressed:)];
    [self setEditing:NO animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    _editing = editing;

    if (editing == YES) {
        [self.table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.data.count -1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }

    [super setEditing:editing animated:animated];
    [self.table setEditing:editing animated:animated];

    if (editing == NO) {
        [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.data.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self fetchChannels];
}

- (void)fetchChannels {
    [self.table reloadData];
}

-(void) loadRecords{
    self.data = @[@"Record 1", @"Record 2", @"Record 3"];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (indexPath.row == self.data.count)? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _editing? self.data.count + 1 : self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = self.data[indexPath.row];
    return cell;
}
@end
@接口zViewController:UIViewController @属性(弱、非原子)IBUITableView*表; @结束 #导入“zViewController.h” @接口zViewController() @属性(非原子,保留)NSArray*数据; @结束 @zViewController的实现{ 编辑; } @综合数据=_数据; @综合表=_表; -(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil { self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 如果(自我){ _编辑=否; } 回归自我; } -(无效)viewDidLoad { [超级视图下载]; self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“编辑”样式:UIBarbuttonItemStyleBorded目标:自操作:@selector(EditButtonAspressed:)]; [自动加载记录]; } -(无效)编辑按钮压缩:(id)发件人{ self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“完成”样式:UIBarbuttonItemStyleBorded目标:自操作:@selector(SaveButtonAspressed:)]; [自设置编辑:是动画:是]; } -(无效)SaveButton压缩:(id)发件人{ self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@“编辑”样式:UIBarbuttonItemStyleBorded目标:自操作:@selector(EditButtonAspressed:)]; [自设置编辑:无动画:是]; } -(void)设置编辑:(BOOL)编辑动画:(BOOL)动画{ _编辑=编辑; 如果(编辑==是){ [self.table insertRowsAtIndexPaths:[NSArray arrayWithObject:[nsindepath indexPathForRow:self.data.count-1第1节:0]]withRowAnimation:UITableViewRowAnimationNone]; } [超级设置编辑:编辑动画:动画]; [self.table setEditing:编辑动画:动画]; 如果(编辑==否){ [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath IndeExpathForRow:self.data.count分区:0]]withRowAnimation:UITableViewRowAnimationNone]; } } -(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径{ [自取通道]; } -(void)获取通道{ [self.table reloadData]; } -(作废)装载记录{ self.data=@[@“记录1”、@“记录2”、@“记录3”]; } #pragma标记-表视图数据源 -(NSInteger)表格视图中的节数:(UITableView*)表格视图 { 返回1; } -(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath{ 返回(indexath.row==self.data.count)?UITableViewCellEditingStyleInsert:UITableViewCellEditingStyleDelete; } -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节 { 返回_编辑?self.data.count+1:self.data.count; } -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 静态NSString*CellIdentifier=@“Cell”; UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.textlab.text=self.data[indexPath.row]; 返回单元; } @结束
动画是正确的。您正在插入/删除
-setEditing:animated:
中的最后一行,因此有两种动画正在进行。

到底是什么问题,我无法从该动画中看出最下面一行动画与所有其他行有多大的不同。当视图退出编辑模式时,第二行到最后一行的红色圆圈图标不会像其他图标一样滑出。在我看来,这个问题与添加和删除一行有关。我还没看过代码。我不明白。在我的
setEditing
功能中,我只添加或删除。什么提示正在添加和删除某些内容?我在别处看到过,但我还是不明白。我的代码应该只执行一个或另一个。有什么我忽略了吗?嗯。我不认为刷新视图与此有关。无论如何,我完全删除了它,动画仍然不匹配。你是把我的代码拉下来,还是仅仅做了一个测试项目,基本上完成了我正在做的事情?如果是这样的话,你能把代码放到网上,我会把它拉下来吗?你能解释一下你的答案而不是仅仅发布代码吗?这和你的代码完全一样。我有你正在处理的所有事件,仅此而已。我只是从您的代码中粘贴了代码,没有做太多更改。唯一的改变是如何获取数据。我有固定数量的数据不是来自服务呼叫,而是来自服务呼叫