Reputation: 1476
I am using a UISwitch subclass to add UISwitches to all my UITableViewCells. I use the custom class to be able to pass more info to the UISwitch.
The error I have on iOS 8 ONLY is:
*** -[NamedUISwitch _sendActionsForEvents:withEvent:]: message sent to deallocated instance
NamedUISwitch is the Custom UISwitch I made:
@interface NamedUISwitch : UISwitch
@property (nonatomic, strong) NSString *specialinfo1;
@property (nonatomic, strong) NSString *specialinfo2;
@end
@implementation NamedUISwitch
@end
This is how I implement my UISwitch in the cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] init];
NamedUISwitch *switchview = [[NamedUISwitch alloc] initWithFrame:CGRectZero];
[switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
cell.textLabel.text = ...;
switchview.nomEtablissement = ...;
switchview.tag = ...;
switchview.typeInfo = ...;
cell.accessoryView = switchview;
return cell;
}
I have tried using Instruments to track the dealloc but I can't seem to get it to work the right way. How can I resolve this dealloc issue?
Upvotes: 0
Views: 162
Reputation: 318794
You are not creating your cells correctly. You need code something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
NamedUISwitch *switchview = nil;
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
switchview = [[NamedUISwitch alloc] initWithFrame:CGRectZero];
[switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = switchview;
} else {
switchview = cell.accessoryview;
}
cell.textLabel.text = ...;
switchview.nomEtablissement = ...;
switchview.tag = ...;
switchview.typeInfo = ...;
// You also need to set switchview.on here
return cell;
}
This way you reuse cells properly and each cell only gets one switch.
An even better option would be to create a custom table cell class and that cell class sets up its own switch.
Upvotes: 1