V.J
V.J

Reputation: 189

iPhone Hide/Show labels in Uitableview cell

I am expanding cell on tapping a cell. Now i want to hide older labels and needs to add new labels.

On hiding a cell contents, it hides the contents from every reusable cell. (like on selecting cell 1 i want to hide older label and show new labels for cell 1, but it hides labels from 1, 6, 11.. row)

I am not able to find the solution.

I am using this code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    

        BOOL isSelected = ![self cellIsSelected:indexPath];

        // Store cell 'selected' state keyed on indexPath
        NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
        [selectedIndexes setObject:selectedIndex forKey:indexPath];        

        [self.newsTableview beginUpdates];

        UITableViewCell *tableviewCell = [tableView cellForRowAtIndexPath:indexPath];
        UITextView *textView = (UITextView *)[tableviewCell.contentView viewWithTag:101];
        UILabel *label = (UILabel *)[tableviewCell.contentView viewWithTag:100];
        UIImageView *imageView1 = (UIImageView *)[tableviewCell.contentView viewWithTag:102];

         if([self cellIsSelected:indexPath])
         {          
             label.hidden = YES;
             imageView1.hidden = YES;
             textView.hidden = YES;           
         }        

        [self.newsTableview endUpdates];                       


    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
}

Please help.. Thanks in advance

Upvotes: 0

Views: 1166

Answers (3)

V.J
V.J

Reputation: 189

I able to fix it using Paras suggestion:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

Upvotes: 0

alper_k
alper_k

Reputation: 512

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

I think you use only one Cell Identifier like this. When you change something, it effects all of them. Try using different identifiers.

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

use dequeueReusableCellWithIdentifier to nil like bellow

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

or assign different identifier like bellow...

   NSString *CellIdentifier =[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Upvotes: 1

Related Questions