質問

I have a UITableView that worked perfectly as desired until I tried to turn every row into sections, and give every sections its own header, kinda like the Instagram app has in its main feed.

so my working code is as follows:

@property (nonatomic) NSMutableArray *postsList;


- (void)addPostsToList:(NSArray *)posts {

    NSInteger startingRow = [self.postsList count];
    NSInteger postsCount = [posts count];
    NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithCapacity:postsCount];
    for (NSInteger row = startingRow; row < (startingRow + postsCount); row++) {

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        [indexPaths addObject:indexPath];
    }

    [self.postsList addObjectsFromArray:posts];
    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:[self.postsList count]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.postList count];
}

But when I changed my code to the following, I get an Assertion Failure:

- (void)addPostsToList:(NSArray *)posts {

    [self.postsList addObjectsFromArray:posts];
    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:[self.postsList count]] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

     return [self.postsList count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 40;
}

This is the error message:

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:1134 * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert section 100 but there are only 100 sections after the update'

Can anyone shed some light on this please ?

役に立ちましたか?

解決

You need to insert a section for each post. Update your original addPostsToList method like this:

- (void)addPostsToList:(NSArray *)posts {
    NSInteger startingSection = [self.postsList count];
    NSRange sectionRange = NSMakeRange(startingSection, posts.count);
    [self.postsList addObjectsFromArray:posts];
    [self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:sectionRange] withRowAnimation:UITableViewRowAnimationAutomatic];
}

他のヒント

you are using the count but you should be using count - 1 index starts at 0

indexSetWithIndex:[self.postsList count...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top