0
votes

I am creating one table based application using TQMultistageTableView library to expand sections. My requirement is if the user will tap on table sections then section background color should change to blue color, rest section background color should be white color if not selected/expanded. For this, I have created an array to save the section index and then I'm reloading this section.

Very first time (when my array is nil), its working fine. But the problem is when there is one section index saved in my array (as any one section is expanded/selected) and then I am trying to expand other section with reloading the second section, that time it's crashing with below error :

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:1368

*** 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 (0) 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).'

Here is my code :

//header open method

- (void)mTableView:(TQMultistageTableView *)mTableView1 willOpenHeaderAtSection:(NSInteger)section
{
    NSLog(@"Open Header ----%ld",section);
    NSDictionary *selectedSection = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld", (long)section] ,@"section" ,nil];

    for(int i=0;i<[ selectedArrayForSectionPlus count];i++)
    {
        if([[[selectedArrayForSectionPlus objectAtIndex:i] objectForKey:@"section"] isEqualToString:[NSString stringWithFormat:@"%ld", (long)section]])
        {
            [selectedArrayForSectionPlus removeObjectAtIndex:i];
            [self.mTableView.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];

            return;
        }
    }

    [selectedArrayForSectionPlus addObject:selectedSection];

    [self.mTableView.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];//here it is crashing
}

//header close method

- (void)mTableView:(TQMultistageTableView *)mTableView willCloseHeaderAtSection:(NSInteger)section
{    
    NSLog(@"Close Header ---%ld",section);

    for(int i=0;i<[ selectedArrayForSectionPlus count];i++)
    {
        if([[[selectedArrayForSectionPlus objectAtIndex:i] objectForKey:@"section"] isEqualToString:[NSString stringWithFormat:@"%ld", (long)section]])
        {
            [selectedArrayForSectionPlus removeObjectAtIndex:i];
            [self.mTableView.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];

            return;
        }
    }
}

//viewForHeaderInSection method

- (UIView *)mTableView:(TQMultistageTableView *)mTableView viewForHeaderInSection:(NSInteger)section;
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.mTableView.frame.size.width, 80)];

    for (int i = 0; i<selectedArrayForSectionPlus.count; i++) {
        if([[[selectedArrayForSectionPlus objectAtIndex:i] objectForKey:@"section"] isEqualToString:[NSString stringWithFormat:@"%ld", (long)section]])
        {

            [view setBackgroundColor:[UIColor colorWithRed:0/255.0 green:150/255.0 blue:214/255.0 alpha:1.0]];

        }
        else
        {
            [view setBackgroundColor:[UIColor whiteColor]];
        }
    }

    return view;
}

Does anyone have any idea how to reload the section?

Thanks for any pointers!

4

4 Answers

2
votes

Encapsulate your section reload calls in beginUpdates and endUpdates something like this:

[self.mTableView.tableView beginUpdates];
[selectedArrayForSectionPlus removeObjectAtIndex:i];
[self.mTableView.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
[self.mTableView.tableView endUpdates];

EDIT: Post discussion with OP.

  1. Tap on section 1 calls willOpenHeaderAtSection. Keep track of the tapped section here.
  2. Tap on section 2 calls willCloseHeaderAtSection. Match the section with the tracked section in #1. If matches, execute your willCloseHeaderAtSection code and reset the tracked section. If not matches simply return without executing your code.
2
votes

When you are reloading a specific section, other sections data source (your data array) should be same as before.

I think you are updating the data source before the reload section call.

Here is your problem

You are not reloading the section 1 but some other. Before reload your data source (data array) contains three rows (records) for section 1 but when you reload the section, because of some reason your data source (data array index 1) is changed to 0 rows (records). index 1 of data source (data array)should contains three rows.

Check your data source when is populated and compare both before and after update.

0
votes

Here is Your Problem:

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

So before using this line add [_mTableView reloadtable];

Because there is a header and tableview if you left to update anything one ,i should be problem .

if your problem solved on the above line then its fine....

0
votes

I had Same Problem When You Open Section 1 Header And Update The Header while opening header At different section it use to throw the error so here is what i did to work it fine code for updating headers

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

and only one thing more you need to do is In the TQMultistageTableView.m File the method name -(void)openOrCloseHeaderWithSection:(NSInteger)section just Paste Below Code And Your done

  [self.tableView beginUpdates];
NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];

NSInteger oldSection = self.openedIndexPath.section;
NSInteger newSection = section;

if (oldSection <= -1)
{

    if (oldSection != newSection)
    {
        insertIndexPaths = [self buildInsertRowWithSection:newSection];
    }
}
else
{

    if (oldSection != newSection && newSection > -1)
    {

        deleteIndexPaths = [self buildDeleteRowWithSection:oldSection];
        insertIndexPaths = [self buildInsertRowWithSection:newSection];
    }
    else
    {
         deleteIndexPaths = [self buildDeleteRowWithSection:oldSection];
    }
}


if ([deleteIndexPaths count] > 0)
{
    [self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
}
if ([insertIndexPaths count] > 0)
{
    [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft];
}

[self.tableView endUpdates];