Adding to matt's answer. When you scroll UITableView
the function below get called everytime, and you are initializing the switch again and again which has already been created and that is actually causing lag in scroll.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
There is a very simple solution for that, follow these steps
Put a UISwitch
in your custom cell xib and follow the instructions in the images below
Create an IBOutlet
of the UISwitch
in the .h class of your CustomCell
, remember to import 'SevenSwitch.h'. When you will create an IBOutlet for the UISwitch, your code should look like below
@property(nonatomic, strong) IBOutlet SevenSwitch *subscribed;
Now your code in cellForRowAtIndexPath
should look like below
/* Switch Inside the cell */
cella.subscribed.offImage = [UIImage imageNamed:@"off.png"];
cella.subscribed.onImage = [UIImage imageNamed:@"on.png"];
cella.subscribed.thumbTintColor = [UIColor colorWithRed:(230/255.0) green:(230/255.0) blue:(230/255.0) alpha:1];
cella.subscribed.activeColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
cella.subscribed.inactiveColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
cella.subscribed.onTintColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
cella.subscribed.isRounded = NO;
cella.subscribed.tag = [[tempCat objectForKey:@"Id"] intValue];
[cella.subscribed addTarget:self action:@selector(changeSingleCategory:) forControlEvents:UIControlEventValueChanged];
if ([[tempCat objectForKey:@"Subscribed"] isEqualToString:@"Y"]) {
cella.subscribed.on = YES;
} else {
cella.subscribed.on = NO;
}
/* End Switch Editing */
You will notice I have removed the first and the last line of your code, so now your switch is getting initialized from xib only and only once, and in the function your are just changing the properties.
Hope it helps.