单击 ios 中的单元格时如何旋转可扩展单元格中的图像?

how to rotate image in expandable cell when clicked on cell in ios?

我正在为 iOS 应用制作一个带有 expandable/collapsible 个单元格的 UITableView。在那个应用程序中,我想显示一个向下箭头图像,当点击它时,图像将是一个向上箭头。这可能吗?

这是我的代码

-(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];
}

这是我的图片,名为 "rotateImage"。请帮我。 这是我的可扩展单元格默认图像:

当用户单击单元格时,图像将是向上箭头,如果用户再次单击单元格,默认图像将再次显示。

如果您要子类化您的 UITableViewCell,您可以使用:setSelected: animated:,在那里添加您的配置。

- (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
}

如果没有,您只需在 -cellForRowAtIndexPath 中使用它,例如:

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

    return tableCell;
}