2

I'm making a UITableView with expandable/collapsible cells for an iOS app. In that app I want to display an arrow down image and when click on it the image will be an up arrow. Is this possible?

Here is my code

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndex == indexPath.row)
    {
        selectedIndex = -1;
        [myContestTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        return;
    }
    if (selectedIndex != -1)
    {
        NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
        selectedIndex = indexPath.row;
        [myContestTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    selectedIndex = indexPath.row;
    [myContestTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

This is my image named "rotateImage". Please help me. Here is my expandable cell default image:

enter image description here

When the user click on a cell the image will be an up arrow and if the user click again on cell default image should display again.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
User558
  • 1,165
  • 1
  • 13
  • 37

1 Answers1

4

If you're subclassing your UITableViewCell you can use: setSelected: animated:, add your configuration there.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    YOURIMAGEVIEW.transform = CGAffineTransformMakeRotation(selected ? M_PI : 0);

    // Configure the view for the selected state
}

if not, you simply use that inside -cellForRowAtIndexPath like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    YOURTABLECELL.yourImageView.tranform = CGAffineTransformMakeRotation((selectedIndex == indexPath.row) ? M_PI : 0);
    ...

    return tableCell;
}
0yeoj
  • 4,500
  • 3
  • 23
  • 41
  • @MangiReddy, you can mark it as the correct answer.. ;) Glad to help. Cheers! – 0yeoj Jan 21 '16 at 08:32
  • @MangiReddy, actually you did not, it's the checkmark, but don't worry about it, what's important is it's working now.. ;) – 0yeoj Jan 21 '16 at 08:54