UITableView的奇怪行为

时间:2014-06-16 14:37:21

标签: ios objective-c uitableview nsuserdefaults

我看到使用Popover显示的UItableViewController有一些奇怪的行为。

这是我的ViewController代码:

@interface PrepareViewController ()
{
NSMutableArray *_objects;
NSString *savingKey;
}

@end

@implementation PrepareViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)awakeFromNib
{
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
[super awakeFromNib];
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

// Register a class or nib file using registerNib:forCellReuseIdentifier
// o registerClass:forCellReuiseIdentifier: method before calling this method
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

// Setup savingKey
savingKey = @"prepareKey";

// Load the items from NSUserDefaults
_objects = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:savingKey]];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:) name:NSUserDefaultsDidChangeNotification object:nil];
}

- (void)defaultsChanged:(NSNotification *)notification {
// Setup savingKey
savingKey = @"prepareKey";

// Load the items from NSUserDefaults
_objects = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:savingKey]];

// Reload the tableView
[self.tableView reloadData];
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)saveSummaries {
// Save the summaries to NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:_objects forKey:savingKey];
}

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _objects.count;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSString *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Open the chosen item
MyManager *sharedManager = [MyManager sharedManager];
sharedManager.openSummary = _objects[indexPath.row];

// Modal to SummaryViewController
[self performSegueWithIdentifier:@"summarySegue" sender:self];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    [_objects removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    // Delete the associated information
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"%@%@", _objects[indexPath.row], @"-summary"]];

    NSLog(@"delete3");

    // Save the new list of combined summaries
    [[NSUserDefaults standardUserDefaults] setObject:_objects forKey:@"prepareKey"];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}

@end

问题: 当用户在编辑模式下点击删除时,不会显示动画,并且所选项目会在没有任何动画的情况下快速回到原位,并且该项目将保持未删除状态。我已经检查过delete方法被调用了 - 任何想法可能是什么问题?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为是因为:

- (void)defaultsChanged:(NSNotification *)notification {
// Setup savingKey
savingKey = @"prepareKey";

// Load the items from NSUserDefaults
_objects = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:savingKey]];

// Reload the tableView
[self.tableView reloadData]; <------- manual reload without animations
}

更改默认值时:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

执行该代码。

相关问题