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 ?
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.
Comments