Justin Damn Moore Justin Damn Moore - 1 month ago 13
iOS Question

UITableViewCell reorder control disappearing when table is scrolled

My UITableView has 2 custom cells: cell1 & cell2

My UITableView displays one or the other depending on an enum value.

If enumValue == 0, cell1 is shown and the table should not allow reordering
if enumValue > 0, cell2 is shown and the table should allow reordering

All that is working fine. The reorder controls are displayed appropriately as the enum values change.

My problem: When the reorder controls appear on the cell, they will disappear when the row is scrolled out of view. Why is this happening?

Sorry about the formatting and rough code. I was haven't issues with the site. Anything obvious stand out that could be causing this?

> // enum
>
> -(void)setScorePhase:(phase)scorePhase {
>
> _scorePhase = scorePhase;
>
> if (scorePhase > score) {
> self.btnPrevious.hidden = NO;
> [self.tableCorps setEditing:YES animated:YES];
> } else {
> self.btnPrevious.hidden = YES;
> [self.tableCorps setEditing:NO animated:YES];
> }
>
> switch (scorePhase) {
> case score:
> self.lblInstructions.text = @"Give your scores";
> self.btnNext.titleLabel.text = @"Next";
> break;
> case bestdrums:
> self.lblInstructions.text = @"Order the corps by best percussion";
> self.btnNext.titleLabel.text = @"Next";
> break;
> case besthornline:
> self.lblInstructions.text = @"Order the corps by best hornline";
> self.btnNext.titleLabel.text = @"Next";
> break;
> case bestguard:
> self.lblInstructions.text = @"Order the corps by best colorguard";
> self.btnNext.titleLabel.text = @"Next";
> break;
> case loudesthornline:
> self.lblInstructions.text = @"Order the corps by loudest hornline";
> self.btnNext.titleLabel.text = @"Next";
> break;
> case favorite:
> self.lblInstructions.text = @"Order the corps by your favorite";
> self.btnNext.titleLabel.text = @"Submit";
> break;
>
> default:
> self.lblInstructions.text = @"Error";
> break;
> }
> [self.tableCorps reloadData];
}




>

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
>
> return 2;
> }
>
> -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
>
> switch (section) {
> case 0: return @"World Class";
> case 1: return @"Open Class";
> default: return @"Error";
> }
> }
>
> - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
>
> if ([self.arrayOfPerformingCorps count]) {
> switch (section) {
> case 0: return [self.arrayOfWorldClass count];
> case 1: return [self.arrayOfOpenClass count];
> default: return 0;
> }
> } else {
> return 0;
> }
> }
>
> -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
>
> UITableViewCell *cell;
> if (self.scorePhase == score) {
> cell = [self.tableCorps dequeueReusableCellWithIdentifier:@"cell1"];
>cell.showsReorderControl = NO;
>
> } else {
> cell = [self.tableCorps dequeueReusableCellWithIdentifier:@"cell2"];
>cell.showsReorderControl = YES;
>
> }
>
>
> PFObject *corps;
> if ([self.arrayOfPerformingCorps count]) {
> if ((int)[indexPath section] == 0) {
> corps = [self.arrayOfWorldClass objectAtIndex:[indexPath row]];
> } else {
> corps = [self.arrayOfOpenClass objectAtIndex:[indexPath row]];
> }
>
> UILabel *corpsNameLabel = (UILabel *)[cell viewWithTag:0];
> corpsNameLabel.text = corps[@"corpsName"];
>
>
> } else {
> //cell.textLabel.text = @"";
> //cell.detailTextLabel.text = @"";
> }
>
> return cell;
> }
>
> #pragma mark - Table Reordering
> - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
> if (self.scorePhase > score) return YES;
> else return NO;
> }
> - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
> return UITableViewCellEditingStyleNone;
> }
> - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
> return NO;
> }
> - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
> return YES;
> }
> - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
> toIndexPath:(NSIndexPath *)destinationIndexPath{
>
> }
>
> - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath
> *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
> {
> if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
> NSInteger row = 0;
> if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
> row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
> }
> return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];
> }
>
> return proposedDestinationIndexPath;
> }

Answer

This is iOS 8 bug. A simple workaround is to override prepareForReuse in the UITableViewCell subclass like this:

- (void)prepareForReuse
{
    [super prepareForReuse];
    [self setEditing:NO animated:NO];
}
Comments