簡體   English   中英

從 UITableView 刪除部分時應用程序崩潰

[英]crash application while delete section from UITableView

tableview我有很多部分,在部分的頂行我有一個刪除按鈕,我想在點擊按鈕后刪除部分但是它崩潰了請幫助我我該怎么做? 這是代碼...

//****** adding delete section button
     CellButton *_sectionButton = (CellButton*)[cell viewWithTag:10];
     _sectionButton.hidden = YES;
     _sectionButton.indexPath = indexPath;
     [_sectionButton addTarget:self action:@selector(sectionDeleteButton:)      forControlEvents:UIControlEventTouchUpInside];

-(void)sectionDeleteButton: (id)sender
{
   CellButton *_secbtn = (CellButton*)sender;
   NSInteger sectionNumber = _secbtn.indexPath.section;
   [self.tableView beginUpdates];
   [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionNumber] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    [self.tableView reloadData];

}

編輯:錯誤消息是:

-[UITableView _endCellAnimationsWithContext:]、/SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1054 2013-07-04 04:25:15.921估計[9025:c07]中的斷言失敗***由於'未捕獲而終止異常應用程序NSInternalInconsistencyException',原因:'無效的更新:無效的節數。 更新后表視圖中包含的節數(2)必須等於更新前表視圖中包含的節數(2),加上或減去插入或刪除的節數(0插入,1已刪除)

我猜numberOfSectionsInTableView:返回之前的節數。 您必須確保您告訴表視圖執行的操作也發生在數據源中。 也就是說,如果您刪除了一個部分,則numberOfSectionsInTableView也必須返回一個比以前小 1 的數字。 這同樣適用於細胞和其他一切。 這可能在您收到的錯誤中有所描述。

您必須管理numberOfSectionsInTableview:的節數numberOfSectionsInTableview:委托方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return  [MLControl shared].arrBuyList.count;//1;//[MLControl shared].arrBuyList.count;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
       // [tableView beginUpdates];
        NSLog(@"indexPath.row=%ld,indexPath.se=%ld",(long)indexPath.row,(long)indexPath.section);
        [[MLControl shared].arrBuyList removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //    [tableView endUpdates];
    } 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
    }   
}

我遇到了這個問題,但原因和你的不同。

當我為 tableView 添加一個部分時,我使用此方法重新加載數據,這也會像您一樣崩潰日志記錄:

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];

為什么我使用這種方法來重新加載我的表? 如果我只刷新單元格,這工作正常,但我忘了我想更改部分。 有關更多信息:重新加載 UITableView 數據的更可靠方法是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM