2013-03-20 36 views
5

Lưu ý: khi tôi nhấn vào hàng, sau đó ứng dụng gặp sự cố.Lỗi xác nhận khi cập nhật tableView

Tôi đang cố gắng triển khai thêm ô mới trên vòi của người dùng. Tôi thấy rằng có một ví dụ tương tự trong cuộc biểu tình xem bảng WWDC 2011. Đây là mã của tôi từ chế độ xem bảng của tôi.

Dưới đây là lỗi:

2013-03-19 20:04:28.672 Project[51229:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070 

Đây là mã của tôi từ xem bảng.

@interface MyPFQueryTableViewController : PFQueryTableViewController <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate> 

@property (nonatomic, strong) NSIndexPath *controlRowIndexPath; 
@property (nonatomic, strong) NSIndexPath *tappedIndexPath; 

@implementation MyPFQueryTableViewController { 

    ListItemObject *listDetail; 
} 

@synthesize controlRowIndexPath; 
@synthesize tappedIndexPath; 


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     PFObject *object = [self.objects objectAtIndex:indexPath.row]; 
     [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      [self loadObjects]; 
     }]; 
    } 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 
    static NSString *CellIdentifier = @"listCell"; 

    PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"listCell"]; 
    if (cell == nil) { 
     cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 


    } 

    // Configure the cell 
    cell.textLabel.text = [object objectForKey:self.textKey]; 
    //cell.imageView.file = [object objectForKey:self.imageKey]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [super tableView:tableView didSelectRowAtIndexPath:indexPath]; 

    //if user tapped the same row twice let's start getting rid of the control cell 
    if([indexPath isEqual:self.tappedIndexPath]){ 
     [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    } 

    //update the indexpath if needed... I explain this below 
    indexPath = [self modelIndexPathforIndexPath:indexPath]; 

    //pointer to delete the control cell 
    NSIndexPath *indexPathToDelete = self.controlRowIndexPath; 

    //if in fact I tapped the same row twice lets clear our tapping trackers 
    if([indexPath isEqual:self.tappedIndexPath]){ 
     self.tappedIndexPath = nil; 
     self.controlRowIndexPath = nil; 
    } 
    //otherwise let's update them appropriately 
    else{ 
     self.tappedIndexPath = indexPath; //the row the user just tapped. 
     //Now I set the location of where I need to add the dummy cell 
     self.controlRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; 
    } 

    //all logic is done, lets start updating the table 
    [tableView beginUpdates]; 

    //lets delete the control cell, either the user tapped the same row twice or tapped another row 
    if(indexPathToDelete){ 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete] 
           withRowAnimation:UITableViewRowAnimationNone]; 
    } 
    //lets add the new control cell in the right place 
    if(self.controlRowIndexPath){ 
     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath] 
           withRowAnimation:UITableViewRowAnimationNone]; 
    } 

    //and we are done... 
    [tableView endUpdates]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([indexPath isEqual:self.controlRowIndexPath]){ 
     return 45; //height for control cell 
    } 
    return 70; //height for every other cell 
} 

- (NSIndexPath *)modelIndexPathforIndexPath:(NSIndexPath *)indexPath 
{ 
    int whereIsTheControlRow = self.controlRowIndexPath.row; 
    if(self.controlRowIndexPath != nil && indexPath.row > whereIsTheControlRow) 
     return [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:0]; 
    return indexPath; 
} 


@end 
+0

Tôi tò mò nếu sự cố xảy ra khi bỏ chọn hàng. – dasblinkenlight

+0

Ah, khi tôi nhấn vào một hàng, gặp sự cố. – STANGMMX

Trả lời

8

Sự cố nằm trong phương thức didSelectRowAtIndexPath của bạn. Bạn có:

[tableView beginUpdates]; 

//lets delete the control cell, either the user tapped the same row twice or tapped another row 
if(indexPathToDelete){ 
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete] 
          withRowAnimation:UITableViewRowAnimationNone]; 
} 
//lets add the new control cell in the right place 
if(self.controlRowIndexPath){ 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath] 
          withRowAnimation:UITableViewRowAnimationNone]; 
} 

//and we are done... 
[tableView endUpdates]; 

Trước khi bạn thực hiện bất kỳ cuộc gọi nào để yêu cầu thêm hoặc xóa bất kỳ hàng nào, bạn phải cập nhật nguồn dữ liệu bằng cách thêm hoặc xóa dữ liệu. Bảng này sẽ kiểm tra xem có bao nhiêu phần và hàng có trước và sau khi bạn thêm/xóa các hàng. Số lượng phần và hàng sau thay đổi phải phản ánh đúng số lượng dữ liệu bạn thêm/xóa với số lượng hàng bạn thêm/xóa.

Và tất nhiên bạn phải triển khai phương thức numberOfRowsInSection.

+1

Tôi đưa vào - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) phần { PFQuery * query = [PFQuery queryWithClassName: @ "ListItem"]; trả về truy vấn.countObjects; } và tôi vẫn đang gặp sự cố tương tự. Bạn có ý nghĩa gì khi cập nhật nguồn dữ liệu của tôi? Đó không phải là những gì [tableView beginUpdates]; làm? – STANGMMX

4

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section của bạn trông như thế nào?

Lỗi này xảy ra khi bạn cố gắng thêm hoặc xóa hàng khỏi UITableView, nhưng số lượng hàng mà bạn yêu cầu nằm trong phần sau khi cập nhật trong phương thức đó không nhất quán với dữ liệu mới cần tải .

Ví dụ: nếu numberOfRowsInSection của bạn luôn trả về 4 và bạn thêm hàng trong phần đó, tableView sẽ muốn nó là 5, nhưng nó sẽ không bị lỗi. Bạn cần theo dõi số lượng hàng trong mỗi phần và trả về số đó.

+0

Ah, tôi thực sự đã không thực hiện điều đó. Tôi đang sử dụng SDK của Parse vì vậy, tôi xem bảng populates với bất cứ điều gì mà tôi đẩy vào phân tích cú pháp. Vì vậy .. Có vẻ như không có gì. – STANGMMX

+0

Trong trường hợp đó, tôi không biết làm thế nào mà SDK hoạt động nhưng chỉ cần đảm bảo bất cứ khi nào bạn chèn một ô vào bảng, bạn đang thêm một cái gì đó theo cách sao cho bảng sẽ có số lượng ô chính xác và giống nhau để xóa. – Jsdodgers

+0

vâng tôi đã làm một số googling và đó có vẻ là chủ đề phổ biến, bệnh kiểm tra xem nó ra. cảm ơn cho các tip – STANGMMX

Các vấn đề liên quan