这有点奇怪,我似乎无法弄明白。 我使用Parse后端并显示TableView来加载数据并滑动以删除它。
代码的工作原理除了一部分: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
如果我将该代码留在应用程序崩溃中,如果我将其注释掉它可以正常工作但没有动画。我检查了放入NSLog语句以打印出indexPath.row以确保我删除并且我有正确的总行数。但是为什么没有动画就可以完美地运行但动画会崩溃?
我收到的错误消息是:
' NSInternalInconsistencyException',原因:'无效更新:无效 第0节中的行数。包含在中的行数 更新后的现有部分(1)必须等于数量 在更新(1)之前,该部分中包含的行加或减 从该部分插入或删除的行数(插入0, 删除1)加上或减去移入或移出的行数 该部分(0移入,0移出)。'
我的代码是:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self retrieveOrders];
}
- (void)viewDidLoad
{
[super viewDidLoad];
PFUser *currentUser = [PFUser currentUser];
if (currentUser)
{
NSLog(@"%@'s Orders", currentUser.username);
}
self.refreshControl = [[UIRefreshControl alloc] init]; // Refresher MBProgress Hud
[self.refreshControl addTarget:self action:@selector(retrieveOrders) forControlEvents:UIControlEventValueChanged]; // Refresher MBProgress Hud
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.ordersArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
PFObject *order = [self.ordersArray objectAtIndex:indexPath.row];
// NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL];
// UIImage *image = [UIImage imageWithData:imageData];
// cell.imageView.image = image;
_updatedAt = order.updatedAt;
cell.imageView.image = [UIImage imageNamed:@"treehouse.png"];
cell.textLabel.text = [NSString stringWithFormat:@"%@", order[@"recepientName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Shipped on %@", self.formatDate];
return cell;
}
- (void)retrieveOrders {
PFQuery *orderQuery = [PFQuery queryWithClassName:@"Orders"];
[orderQuery orderByAscending:@"createdAt"];
[orderQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
self.ordersArray = objects;
[self.tableView reloadData];
}
if ([self.refreshControl isRefreshing]) {
[self.refreshControl endRefreshing];
}
}];
}
-(NSString *) formatDate{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
[dateFormatter stringFromDate:_updatedAt];
NSString *newTime = [dateFormatter stringFromDate:[[NSDate alloc]init]];
return newTime;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
PFObject *removeObject = [self.ordersArray objectAtIndex:indexPath.row];
NSString *deleteId = removeObject.objectId;
PFObject *object = [PFObject objectWithoutDataWithClassName:@"Orders" objectId:deleteId];
[object deleteInBackground];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Animate the deletion from the table.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"indexPath.row = %ld", (long)indexPath.row);
[self.tableView reloadData];
}
}
答案 0 :(得分:0)
在致电retrieveOrders
后,您永远不会致电deleteInBackground
,以便self.ordersArray
可能仍然保留对您“已删除”对象的引用。 deleteRowsAtIndexPaths:withRowAnimation:
期望行数不同。
此外,deleteInBackground
在后台运行,因此调用retrieveOrders
可能甚至无法完全解决您的问题。
修改强>
你也可以试试这个......
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *removeObject = [self.ordersArray objectAtIndex:indexPath.row];
NSString *deleteId = removeObject.objectId;
PFObject *object = [PFObject objectWithoutDataWithClassName:@"Orders" objectId:deleteId];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"indexPath.row = %ld", (long)indexPath.row);
}
}];
}
}