MikeQ
MikeQ

Reputation: 1817

Animate removal of UITableViewCellAccessory

Is there any way to animate the removal of a UITableView cell accessory?

I currently am showing a UITableViewCellAccessoryDisclosureIndicator, but I would like to animate swapping the disclosure indicator with a UISwitch on all visible table cells.

I've tried something like this:

[UIView animateWithDuration:0.3
                 animations:^{
                   for (SwitchTableViewCell *cell in self.tableView.visibleCells)
                   {
                     cell.accessoryType = UITableViewCellAccessoryNone;
                   }                     
                 }];

... but unfortunately that has no affect. The disclosure indicator abruptly disappears and the contentView width jumps in one step, rather than a smooth transition.

Upvotes: 3

Views: 1157

Answers (1)

ughoavgfhw
ughoavgfhw

Reputation: 39905

accessoryType is not an animatable property. There are two ways you can do this, depending on your situation. The easiest only applies if you are changing the accessory to a UISwitch because of entering the editing state. In this case, just usecell.editingAccessoryType = theSwitch; in your tableView:cellForRowAtIndexPath: method. The table view will then do a fade in/out automatically when entering editing mode.

If you are doing this outside of editing mode, then the following code will do what you want:

[UIView animateWithDuration:0.3 animations:^{
    for(SwitchTableViewCell *cell in self.tableView.visibleCells) {
        [[cell valueForKey:@"_accessoryView"] setAlpha:0.0];
    }
} completion:^(BOOL done) {
    for(SwitchTableViewCell *cell in self.tableView.visibleCells) {
        cell.accessoryView = theSwitch;
    }
}];

However, I do not know if this code will make it into the app store since it uses the hidden property _accessoryView.

Upvotes: 3

Related Questions