Search code examples
iphoneiosuitableviewuiswitchaccessoryview

UISwitch in a custom accessoryView



I have my UITableViewCell subclass in which I use an UISwitch as accessoryView in this way:
mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
self.accessoryView = mySwitch;
and all is ok! The app works fine.

Now I need to add some UIImageView above the switch, so I thought "Well, let's make a custom accessoryView!":
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 10.0f, 100.0f, 60.0f)];
...
mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 94.0f, 27.0f)];
[myView addSubview:mySwitch];
self.accessoryView = myView;
[myView release];

All seems to be ok, but there is a strange behavior. When I open another viewcontroller and get back to the table the switches are mysteriously changed...
It's not a problem in data management, but only in the cell redraw... Please, help me, what can I do?

Thanks in advance


Solution

  • Emh... I found the problem... it was not linked to table redraw...
    On UIControlEventValueChanged the selector retrieves the switch value and the cell indexPath.row in this way:
    UISwitch *tempSwitch = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)[tempSwitch superview];

    and then it updates the corresponding object (of a data logic class) saved in a NSMutableArray (the datasource of the tableview)

    But now the switch is a not the accessoryView, but a subview of the accessoryView, so the object is updates in a unpredictable way. I solved with a second superview message.

    Sorry for my mistake and thanks to all...