簡體   English   中英

動態將圖像添加到原型自定義單元中

[英]Dynamically add images to a prototype custom cell

我有一個帶有嵌入式tableview的UIViewController。 原型單元已經過定制(並細分),以添加由平移手勢識別器啟動的“滑塊”效果。 我已按照本教程進行操作: https : //github.com/spilliams/sparrowlike在單元格內,我有兩個視圖,一個正視圖滑出,而一個后視圖則顯示其他控件。

現在,我需要向每個單元格添加一個圖像,該圖像將根據BOOL變量在兩個圖像之間選擇。

問題是:如果我以編程方式添加圖像視圖,則圖像將被添加到單元格中,然后當用戶滑動前視圖時,圖像將停留在該位置。 因此,應該將圖像添加到前視圖,但是我無法在StoryBoard中添加圖像,也不能在其上添加插座。

所以問題是:我是否應該將代碼放在自定義單元格子類中以檢索圖像? 如果是這樣,我應該放在哪里? 目前,我的“ CustomCell”類實現文件如下所示:

#import "CustomCell.h"

@implementation CustomCell
@synthesize frontView;
@synthesize backView;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

那這樣的東西呢?

- (void)setupCell {
    if (yourBoolValueHere) {
        [[self frontView] setImage:[UIImage imageNamed:someImageName1]];
    } else {
        [[self frontView] setImage:[UIImage imageNamed:someImageName2]];
    }
    [[self view] bringSubviewToFront:[self frontView]];
}

然后,您可以在tableView:cellForRowAtIndexPath:調用[cell setupCell]

暫無
暫無

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

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