tableView刷新section的崩潰

崩潰日志

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

簡(jiǎn)單說(shuō)明一下這個(gè)崩潰造成的原因有兩種:
1、刷新section
2、move section

刷新section

UITableView調(diào)用刷新某一section,如下:

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

而在刷新某一section的時(shí)候,其他的seciton中對(duì)應(yīng)的row個(gè)數(shù)改變侥衬,從而導(dǎo)致崩潰。
下面可以附上一個(gè)簡(jiǎn)單的栗子,一個(gè)類(lèi)搞定的事就不給demo了哈:

#import "ViewController.h"

@interface ViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *dataArray2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    self.dataArray = [NSMutableArray arrayWithObjects:@1,@2,@3,nil];
    self.dataArray2 = [NSMutableArray arrayWithObjects:@1,@2,@3,nil];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.dataArray2 removeObjectAtIndex:0];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    });
}


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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.dataArray.count;
    }
    return self.dataArray2.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *testCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"~~~~~UITableViewCell"];
    testCell.backgroundColor = [UIColor blueColor];
    return testCell;
}

@end

move cell

這邊主要是在UITableView進(jìn)行move section的時(shí)候沒(méi)有注意就會(huì)造成該崩潰乾戏。簡(jiǎn)單說(shuō)明一下UITableView 的move sectionnnn操作,由于move的時(shí)候沒(méi)有進(jìn)行刷新會(huì)導(dǎo)致一個(gè)問(wèn)題:section對(duì)應(yīng)的indexPath還是原本的值三热,UITableView沒(méi)有更新該值,所以需要自己去記錄indexPath的值。而在move的時(shí)候indexPath的section其中一個(gè)值沒(méi)有更新自己記錄的值邢隧,多次move的時(shí)候就會(huì)造成該崩潰痕慢。
附上簡(jiǎn)單的源碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *headerID = @"headerID";
    ZTGClassSortHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    if (!headerView) {
        headerView = [[ZTGClassSortHeaderView alloc] initWithReuseIdentifier:headerID];
    }
    ZTGResponseGradeListSortEnvelop *gradeEnvelop = [self.sortArray zt_safeObjectAtIndex:section];

    ZTGClassSortHeaderItem *haderItem = [[ZTGClassSortHeaderItem alloc] init];
    haderItem.gradeString = gradeEnvelop.gradeName;
    haderItem.showUp = section == 0 ? NO : YES;
    haderItem.showDown = section == self.sortArray.count - 1 ? NO : YES;
    haderItem.section = section;
    headerView.headerItem = haderItem;
    @weakify(self);
    haderItem.didSelectedUp = ^(NSInteger indexSection) {
        @strongify(self);
        NSInteger toSection = indexSection - 1;
        if (toSection >= 0 && toSection < self.sortArray.count) {
            [self moveSectionFrom:indexSection toSection:toSection];
        }
    };
    haderItem.didSelectedDown = ^(NSInteger indexSection) {
        @strongify(self);
        NSInteger toSection = indexSection + 1;
        if (toSection < self.sortArray.count && toSection >= 0) {
            [self moveSectionFrom:indexSection toSection:toSection];
        }
    };

    return headerView;
}

- (void)moveSectionFrom:(NSInteger)section toSection:(NSInteger)toSection {
    self.sortChange = YES;
    // 更改數(shù)據(jù)
    [self.sortArray exchangeObjectAtIndex:section withObjectAtIndex:toSection];
    // 移動(dòng)section
    [self.sortTableView moveSection:section toSection:toSection];
    // 刷新section
    ZTGClassSortHeaderView *headerView = (ZTGClassSortHeaderView *)[self.sortTableView headerViewForSection:section];
    [self updateSectionStateWithHeader:headerView section:section];

    ZTGClassSortHeaderView *toView = (ZTGClassSortHeaderView *)[self.sortTableView headerViewForSection:toSection];
    // 此處注釋后會(huì)出現(xiàn)該崩潰,因?yàn)閷?duì)應(yīng)的section沒(méi)有更新
    //[self updateSectionStateWithHeader:toView section:toSection];
}

- (void)updateSectionStateWithHeader:(ZTGClassSortHeaderView *)headerView section:(NSInteger)section {
    if (headerView.headerItem.updateHeaderState) {
        BOOL isUp = section == 0 ? NO : YES;
        BOOL isDown = section == self.sortArray.count - 1 ? NO : YES;
        headerView.headerItem.updateHeaderState(isUp, isDown, section);
    }
}

headerView.headerItem.updateHeaderState(isUp, isDown, section);目標(biāo)section不做更新抑堡,再次移動(dòng)時(shí)就會(huì)出現(xiàn)改崩潰催跪。
上面的源碼是tableView section的一部分操作,具體的可以自己寫(xiě)個(gè)簡(jiǎn)單的demo夷野。

解決

針對(duì)以上的崩潰就只能是檢查源碼了懊蒸,一般而言UITableView哪里增、刪section(或row)就需要對(duì)其進(jìn)行刷新悯搔,如果操作上都是在主線程的話一般而言都是不出會(huì)有問(wèn)題的骑丸,因?yàn)橹骶€程上是串行。而在異步的時(shí)候增妒貌、刪section(或row)的時(shí)候就需要注意了通危。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市灌曙,隨后出現(xiàn)的幾起案子菊碟,更是在濱河造成了極大的恐慌,老刑警劉巖在刺,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逆害,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蚣驼,警方通過(guò)查閱死者的電腦和手機(jī)魄幕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)颖杏,“玉大人纯陨,你說(shuō)我怎么就攤上這事。” “怎么了翼抠?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵咙轩,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我阴颖,道長(zhǎng)活喊,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任膘盖,我火速辦了婚禮胧弛,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘侠畔。我一直安慰自己结缚,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布软棺。 她就那樣靜靜地躺著红竭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪喘落。 梳的紋絲不亂的頭發(fā)上茵宪,一...
    開(kāi)封第一講書(shū)人閱讀 51,554評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音瘦棋,去河邊找鬼稀火。 笑死,一個(gè)胖子當(dāng)著我的面吹牛赌朋,可吹牛的內(nèi)容都是我干的凰狞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼沛慢,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼赡若!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起团甲,我...
    開(kāi)封第一講書(shū)人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤逾冬,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后躺苦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體身腻,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年圾另,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了霸株。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡集乔,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情扰路,我是刑警寧澤尤溜,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站汗唱,受9級(jí)特大地震影響宫莱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜哩罪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一授霸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧际插,春花似錦碘耳、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至瑟枫,卻和暖如春斗搞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背慷妙。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工僻焚, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人膝擂。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓虑啤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親猿挚。 傳聞我的和親對(duì)象是個(gè)殘疾皇子咐旧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355