簡體   English   中英

具有IBOutlet對象的Xcode自定義單元格

[英]Xcode Custom Cell With IBOutlet Objects and Not

我創建了一個自定義單元:

#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通過情節提要板添加到單元格中,但在方法中添加了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對象,並通過情節提要添加圖像和標簽? 還是最好將cellaMain.m文件中的所有對象(標簽,圖像和SeveSwitch)添加到我的單元格視圖中?

問題是你在說

 addSubview:cella.subscribed

對於每個細胞 但是細胞被重用。 因此,即使已添加此子視圖,您也要添加它。 您需要使所有這些代碼成為條件代碼。 如果子視圖已經存在,請不要添加它。

添加到馬特的答案。 滾動UITableView ,每次都會調用下面的函數,並且您一次又一次初始化開關,該開關已經創建,並且實際上導致滾動滯后。

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

為此,有一個非常簡單的解決方案,請按照以下步驟操作

UISwitch放入您的自定義單元xib中,並按照下圖中的說明進行操作

在此處輸入圖片說明

在此處輸入圖片說明

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初始化了開關,而且只有一次,並且在函數中您只是在更改屬性。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM