簡體   English   中英

自定義UITableViewCell中的iOS自定義UIView初始化和默認屬性

[英]iOS Custom UIView in Custom UITableViewCell Initialization and default properties

我有一個子類UIView控件(MyCustomProgressView),我正在編寫該控件以用於多個應用程序中,每個應用程序都將帶有商標,並且想知道如何使該控件對我的團隊更加用戶友好。 該控件是一個自定義進度視圖,通常將在自定義UITableViewCell中使用。

MyCustomProgressView具有多個屬性,可以將其設置為每次使用時都具有外觀和行為。 我給了它一個初始化器,將它們設置為默認值,但希望控件在使用時尊重這些默認值的覆蓋。

例如,在MyCusomProgressView.m中:

- (void)awakeFromNib {
    [self setup];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    //initialization
    [self setDelegate:nil];
    [self setCurrentProgress:0.0f];
    [self setLastProgress:0.0f];
    [self setAnimated:YES];
    [self setProgressColor:[UIColor redColor]];
    [self setProgressWidth:7.0f];
    [self setTrackWidth:5.0f];
    [self setDuration:0.2f];
    [self setProgressLabelColor:[UIColor: darkGrayColor];
}

大。 現在,當我使用MyCustomTableViewCell中的視圖時,我想這樣做:

@property (strong, nonatomic) IBOutlet MyCusomProgressView *myProgView;
...

- (void)awakeFromNib {
    // Initialization code
    [self.myProgView setDuration:0.25f];
    [self.myProgView setProgressColor:[UIColor blueColor];
    [self.myProgView setProgressLabelColor:[UIColor yellowColor];
    [self.myProgView setTrackWidth:2.0];
    [self.myProgView setProgressWidth:3.0];
}

問題在於,當MyCustomTableViewCell加載MyCusomProgressView時,默認的初始化程序將運行並覆蓋自定義設置。 我知道我可以為每次使用修改MyCusomProgressView.m,但我希望盡可能保持靜態。

萬一有問題,涉及非xib,因為我已經在情節提要中完成了所有工作,並且除了更改自定義單元格類的簽名之外,在cellForRowAtIndexPath中我沒有做任何奇怪的事情:

- (MyCustomTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomTableViewCell *cell = (MyCustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];   
    // Configure the cell...
    [cell.myProgView setDelegate:self];
    return cell;
}

有任何想法嗎? 我敢肯定這很愚蠢...

這是設置方法調用順序的問題。 MyCusomProgressView設置斷點,以查看何時調用awakeFromNibinitWithFrame 還要在MyCustomTableViewCell設置斷點,以查看何時調用其awakeFromNib

如果要調用initWithFrame ,請嘗試在MyCusomProgressView完全刪除awakeFromNib

或者嘗試將此行添加到MyCustomTableViewCell awakeFromNib作為第一行代碼:

    [self.myProgView view];

這將強制加載進度視圖,調用其awakeFromNib 缺點是您不希望進度視圖的用戶必須這樣做,因為它並不明顯。

暫無
暫無

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

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