Ios 带有IBOutlet对象的Xcode自定义单元格,而不是

Ios 带有IBOutlet对象的Xcode自定义单元格,而不是,ios,xcode,uitableview,lag,Ios,Xcode,Uitableview,Lag,我创建了一个自定义单元格: #import <UIKit/UIKit.h> #import "SevenSwitch.h" @interface cellaMain : UITableViewCell { SevenSwitch *subscribed; } @property (nonatomic, retain) IBOutlet UIImageView *imageMain; @property (nonatomic, retain) IBOutlet UILa

我创建了一个自定义单元格:

#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
使用此代码:

    /* 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 */
问题是卷轴滞后了很多。 如何在cellaMain.m中添加SevenSwitch对象,并让图像和标签由情节提要添加?
或者最好将我的cellaMain.m文件中的所有对象(标签、图像和SeveSwitch)添加到我的cell视图中?

问题是你说

 addSubview:cella.subscribed

每一个细胞。但是细胞可以重复使用。因此,即使已添加此子视图,也要添加它。您需要使所有代码都有条件;如果子视图已经存在,则不要添加它。

添加到matt的回答中。当您滚动
UITableView
时,下面的函数每次都会被调用,并且您会一次又一次地初始化已创建的开关,这实际上会导致滚动延迟

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
有一个非常简单的解决方案,请遵循以下步骤

在自定义单元格xib中放置
ui开关
,然后按照下图中的说明操作

CustomCell的.h类中创建
UISwitch
IBOutlet
,记住导入“SevenSwitch.h”。当您为UISwitch创建IBOutlet时,您的代码应该如下所示

@property(nonatomic, strong) IBOutlet SevenSwitch *subscribed;
/* 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 */
现在,
cellforrowatinexpath
中的代码应该如下所示

@property(nonatomic, strong) IBOutlet SevenSwitch *subscribed;
/* 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 */
您会注意到我已经删除了代码的第一行和最后一行,因此现在您的开关仅从xib初始化一次,并且在函数中您只是更改了属性


希望有帮助。

明天我一到办公室就尝试这个解决方案……我认为这可能是正确的解决方案……我忍不住要等到明天……一切都很顺利,SevenSwitch是一个UIControl类对象,因此在故事板中,我添加了一个UIView,而不是UISwitch,分配给它SevenSwitch类…非常感谢…很高兴能帮助您。。干杯。:-)