Assertion failure while trying to expand UITableView cell

SteBra

I want to Expand UITableViewCell on click.

I done that in an app before, using same steps as I am using now, but I guess I'm overseeing something, and I cannot figure out what it is.

I want to display some headlines, stored in a mutable array

NSMutableArray* topicsArray;
_topicsArray = [[NSMutableArray alloc]init];
[_topicsArray addObject:@"Percent"];
[_topicsArray addObject:@"Decimal numbers and fractions"];
[_topicsArray addObject:@"Variables and expressions"];
[_topicsArray addObject:@"Geometry - Area and perimeter"];
[_topicsArray addObject:@"Mathematics in use"];
[_topicsArray addObject:@"Reduction of expression"];
[_topicsArray addObject:@"Chance and probability"];
[_topicsArray addObject:@"Academic reading - Find century"];

This is the code I use for expanding / shrinking cells on click:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.topicsArray containsObject:indexPath])
{
    [self.topicsArray removeObject:indexPath];
}
else
{
    [self.topicsArray addObject:indexPath];

}
[_tableView beginUpdates];
[_tableView endUpdates];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat kExpandedCellHeight = 210;
CGFloat kNormalCellHeigh = 80;

if ([self.topicsArray containsObject:indexPath])
{
    return kExpandedCellHeight;
}
else
{
    return kNormalCellHeigh; 
}
}

When I made my custom cell, i selected "Clears Graphics Content" and "Clip subviews".

The error occurs when I click the cell. Line of code where tit happens is on

[_tableView endUpdates];

in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.

Also, message I get is:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (8), 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).'

What could be the source of the problem ?

SteBra

I actually figured out how to fix this issue.

After you're done adding all the objects to an array, from which you're gonna populate the data in tableView, save that array count, as a constant.

@property int counter;

_counter = _array.count;

Then, in numberOfRowsInSection, pass that counter:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _counter;
}

And voila, problem solved.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assertion failure in UITableView Swift

Assertion failure in UITableView configureCellForDisplay:forIndexPath:

When delete cell and [self loadObjects] in PFQueryTableViewController, it gets Assertion failure in -[UITableView _endCellAnimationsWithContext:]

Assertion failure in -[UITableView _addScrollViewScrollObserver:] while using large title navigation ios 11

Assertion Failure in UITableView - Cant locate the problematic part

Assertion failure UITableView: delete object which a relationship to another object

Strange UITableView error: "Assertion Failure" on dequeuing static cells in storyboard

How to Expand UITableview cell without collapse other cell(multiple cell expand) in swift

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:5261

Failure while trying to append in a try statement

UITableView - cell images changing while scrolling

UITableView() not showing in UIViewController while trying to add it programmatically

Failed assertion while trying to get single document from firebase

Error in groovy script while trying to add xpath assertion

Nullpointer Error in groovy script while trying to add xpath assertion

opencv -215:Assertion failed while trying to print face coorrdenates

Error while trying to store an array to dataframe cell

Assertion failure in UITextView _firstBaselineOffsetFromTop

*** Assertion failure in -[UIView _addConstraint:],

Assertion Failure in UITableViewController

Metal makeComputeCommandEncoder assertion failure

Mysterious type assertion failure?

iOS Assertion Failure in UICollectionView

Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

Assertion failure with segues

Assertion failure in UICollectionView?

Assertion failure in -[NSLayoutConstraint constant]

Assertion failure in GMUNonHierarchicalDistanceBasedAlgorithm clustersAtZoom

UICollectionView Assertion failure in createPreparedCellForItemAtIndexPath