سؤال

I created a Custom Cell:

#import <UIKit/UIKit.h>
#import "SevenSwitch.h"

@interface cellaMain : UITableViewCell {

    SevenSwitch *subscribed;

}

@property (nonatomic, retain) IBOutlet UIImageView *imageMain;
@property (nonatomic, retain) IBOutlet UILabel *titleMain;
@property (nonatomic, retain) SevenSwitch *subscribed;

@end

The UIImage and the Label are added to cell by the storyboard but the sevenswitch is added in the method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

With this code:

    /* Switch Inside the cell */
    cella.subscribed = [[SevenSwitch alloc] initWithFrame:CGRectMake(cella.frame.size.width-60, cella.frame.size.height / 2 - 12, 50, 25)];
    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;
    }

    [cella.contentView addSubview:cella.subscribed];
    /* End Switch Editing */

The problem is that the scroll lags a lot. How can I do to add the SevenSwitch object in the cellaMain.m and let the image and the label be added by the Storyboard? Or maybe is it better add to my cell view all the objects (Label, Image and SeveSwitch) in my cellaMain.m file?

هل كانت مفيدة؟

المحلول

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

enter image description here

enter image description here

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.

نصائح أخرى

The problem is that you are saying

 addSubview:cella.subscribed

for every cell. But cells are reused. So you are adding this subview even if it has already been added. You need to make all of that code conditional; don't add the subview if it is already there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top