gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:46:06 25 4
gpt4 key购买 nike

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

#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

UIImage 和 Label 由 Storyboard添加到单元格,但 sevenswitch 是在方法中添加的:

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

使用这段代码:

    /* 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 对象并让 Storyboard 添加图像和标签?或者将我的 cellaMain.m 文件中的所有对象(标签、图像和 SeveSwitch)添加到我的单元格 View 中是否更好?

最佳答案

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

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

有一个非常简单的解决方案,按照以下步骤操作

在您的自定义单元格 xib 中放置一个 UISwitch 并按照下图中的说明进行操作

enter image description here

enter image description here

CustomCell 的.h 类中创建UISwitchIBOutlet,记得导入'SevenSwitch.h'。当您将为 UISwitch 创建一个 IBOutlet 时,您的代码应如下所示

@property(nonatomic, strong) IBOutlet SevenSwitch *subscribed;

现在 cellForRowAtIndexPath 中的代码应该如下所示

/* 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 初始化一次,并且在函数中您只是更改属性。

希望对您有所帮助。

关于ios - 带有 IBOutlet 对象而不是 IBOutlet 对象的 Xcode 自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22360221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com