0

我有一个 UITableView 我正在使用我的服务器中的 plist 进行更新。我想使用以下方法在 tableview 中插入和删除部分:

[global.tableVC.tableViewOutlet insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
[global.tableVC.tableViewOutlet deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];

问题是我拥有的不同部分也有很多行,所以当我更新 tableview 时总是会出现这个错误:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070
2013-05-11 13:16:39.946 uninkd[19619:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 3.  The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (1), 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).'

现在,我知道它在这里告诉我什么,突然第 3 节有 0 行而不是 1 行,但我真的不知道如何处理它。

我是否必须遍历所有部分,检查该部分是否有行,然后...删除这些行并将它们插入下面?移动它们?

这是我更新表格视图的完整代码:

[global.tableVC.tableViewOutlet beginUpdates];

// Update data
NSString *manifestPlistPath = [global.documentsDirectory stringByAppendingPathComponent:@"Manifest.plist"];
[plist writeToFile:manifestPlistPath atomically:YES];

// Insert new section
[global.tableVC.tableViewOutlet insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];

[global.tableVC.tableViewOutlet endUpdates];

更新问题和 cellForRowAtIndexPath:

-(NSMutableArray *)issues{
    // 1. Initiate issues array
    _issues = [[NSMutableArray alloc] init];

    // 2. Define and set magazine and foreword
    NSString *magazinePath = [[NSBundle mainBundle] pathForResource:@"Magazine" ofType:@"plist"];
    NSString *forewordPath = [[NSBundle mainBundle] pathForResource:@"Foreword" ofType:@"plist"];

    // 3. If we have any issues, then reset the magazine and foreword path
    if (kLibrary.issues.count) {
        // A. Get the last issue
        NKIssue *lastIssue = [kLibrary.issues lastObject];

        // B. Redefine the magazine and foreword path
        magazinePath = [[lastIssue.contentURL path] stringByAppendingPathComponent:@"Magazine.plist"];
        forewordPath = [[lastIssue.contentURL path] stringByAppendingPathComponent:@"Foreword.plist"];
    }

    // 5. If file exists at that path, then set the manifestplistdic
    if ([self manifest].count){
        // B. Add the individual issues to the issues global
        for (NSMutableDictionary __strong *issue in [self manifest]){
            NSString *issueName = [[issue objectForKey:@"Issue"] objectForKey:@"Name"];
            NKIssue *libraryIssue = [kLibrary issueWithName:issueName];
            if (libraryIssue) {
                NSString *issuePath = [[libraryIssue.contentURL path] stringByAppendingPathComponent:@"Issue.plist"];
                issue = [[NSMutableDictionary alloc] initWithContentsOfFile:issuePath];
            }

            [_issues addObject:issue];
        }
    }

    // 6. Add the magazine and foreword
    [_issues insertObject:[[NSDictionary alloc] initWithContentsOfFile:magazinePath] atIndex:0];
    [_issues addObject:[[NSDictionary alloc] initWithContentsOfFile:forewordPath]];

    // 7. Return the array
    return _issues;
}


- (DTAttributedTextCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (DTAttributedTextCell *)[self tableView:tableView preparedCellForIndexPath:indexPath];
}

- (DTAttributedTextCell *)tableView:(UITableView *)tableView preparedCellForIndexPath:(NSIndexPath *)indexPath{
    // 1. Get cell cache if cell has already been created
    CustomCellView *cell = [global rowAtIndexPath:indexPath];

    // 2. Create cell if it doesn't exist
    if (!cell){
        // A. Define cell string (for first row in buy section
        NSString *cellString = @"";

        // C. If we have articles, then reset cell string
        if([global sectionAtSection:indexPath.section].bought){
            // A. Find the right issue and article
            NSDictionary *article = [global articleAtIndexPath:indexPath];

            // B. Set the cell string and put it into an attributed string
            cellString = [NSString stringWithFormat:@"%@ %@ %@", [article objectForKey:@"Headline"], [article objectForKey:@"Byline"], [article objectForKey:@"Lead"]];
        }

        // D. Create the cell from the cell identifier
        cell = [[CustomCellView alloc] initWithIndexPath:indexPath title:cellString];

        // E. Cache the cell, if there is a cache
        [_rowCache setObject:cell forKey:[global cellKeyFromIndexPath:indexPath]];
    }

    // 3. Return the cell
    return cell;
}
4

0 回答 0