簡體   English   中英

具有UIImageView的自定義UITableViewCell不為某些單元格加載圖像

[英]Custom UITableViewCell with a UIImageView not loading image for certain cells

我有這個非常奇怪的問題。

基本上,我有一個UITableViewCell子類,該子類通過ive編寫並將某些插座鏈接到Nib。

Cell.h的子類

@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

Cell.m的子類

@synthesize myImageView = _myImageView; // do i need this?


- (void) setUpCell... image:(UIImage*)image
{
       if(image)
        self.myImageView.image = image;
    else
        self.myImageView.image = nil;
} 

在管理單元格表視圖的視圖控制器中,

我已經使用以下代碼向Nib注冊:

[self.tableView registerNib:[UINib nibWithNibName:pathToNibForBaseMenuCell bundle:nil] forCellReuseIdentifier:identifierForBaseMenuCell];

在cellForRowAtIndexPath中,我將單元出隊並設置圖像...

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell];

switch (indexPath.section) {


    case 2:
        [cell setUpCell...image:self.cogWheelImageForSettingsIcon];
        break;

    default:
        break;
}

return cell;

}

奇怪的是,假設我在第2節中有10個單元格(如上所述),並且以相同的方式設置它們,有些單元格顯示了齒輪圖標,有些則沒有! 將這些單元格滾動進和移出視圖似乎可以更改哪些單元格顯示圖標,哪些不顯示圖標。

這意味着注冊單元格,包括設置重用標識符,設置nib文件中的子類,所有這些事情都很好(常見的陷阱)。

在我看來,這種行為模式與內存有關-好像某個東西被釋放了。

這是一張圖片來說明我的意思:

在此處輸入圖片說明

請幫忙!

謝謝

Vb

編輯:

好的,只是要明確一點,我尚未在此表中使用任何數據,但是盡管如此,我沒有理由不應該出現這些齒輪!

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
switch (section) {
    case 0:
        return 1;
        break;

    case 1:
        return 10;
        break;

    case 2:
        return 10;
        break;

    default:
        return 0;
        break;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell];

switch (indexPath.section) {
    case 0:
        [cell setUpCell... text:@"Username" image:nil];
        break;

    case 1:
        [cell setUpCell... text:@"Magazine" image:nil];
        break;

    case 2:
    {
        NSString* str = [NSString stringWithFormat:@"Settings: %d",indexPath.row];
        [cell setUpCell... text:str image:self.cogWheelImageForSettingsIcon];
        break;
    }

    default:
        break;
}

return cell;
}

的輸出是...

在此處輸入圖片說明

我的觀點是,如果我為第二部分“設置”中的所有單元格設置了相同的圖像,為什么只顯示其中一些圖像?

自定義單元格類:

@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
@property (weak, nonatomic) IBOutlet UILabel *mainLabel;
@property (weak, nonatomic) IBOutlet UILabel *arrowView;
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- (void) setUpCelltext:(NSString*)text image:(UIImage*)image;


@implementation CustomCell

- (void) setUpCelltext:(NSString*)text image:(UIImage*)image
{
self.backgroundImageView.image = [UIImage imageNamed:pathForBackgroundImage];
self.myImageView.layer.cornerRadius = kCornerRadiusOfImageView;
self.myImageView.layer.masksToBounds = YES;

if(image)
    self.myImageView.image = image;
else
    self.myImageView.image = nil;

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:self.fontForText forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary];
self.mainLabel.attributedText = attrString;
}
@end

單元具有自定義字體,箭頭視圖,背景視圖-它們都可以正常工作! 它們根據部分而變化。 它只是圖像不起作用。 您可以看到由cellForRowAtIndexPath調用的此函數將設置圖像(如果提供),或者將圖像視圖設置為nil(如果未提供)。

原來,問題與在筆尖中使用“ imageView”作為imageView的名稱有關。 后來進行了更改,但原始關系是UITableViewCell的實例變量而不是Custom Cell。

故事的寓意:不要將自定義表格視圖單元格的圖像視圖稱為“ imageView”!

使用dequeueReusableCellWithIdentifier:從舊單元格重繪UITableViewCell 如果您不想重復使用這些單元格,則每次在tableView:cellForRowAtIndexPath:函數中都需要初始化一個新單元格。

但是,如果要重用單元格(推薦),則應該每次在tableView:cellForRowAtIndexPath:內部調用- (void) setUpCell... image:(UIImage*)image函數,以確保圖像僅設置為正確的單元格。

您似乎還使用了存儲在UITableViewCell子類中的數據,但是當單元格在另一行上重用時,它仍會認為它與原始行是同一單元格。

例如:在第2行中創建一個單元格,並具有圖標圖像。 單元2不在屏幕上,現在用於第16行。第16行不應有圖像,但是該單元仍然認為它是第2行中的同一單元,並繼續顯示其圖像。

如果您想重用每個單元格並為每個單元格提供唯一的數據,請僅使用外部數據集(例如NSArray ),該數據集提供繪制單元格並將信息每次傳遞給單元格所需的信息。 ,而不是根據單元創建時提供的信息。

暫無
暫無

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

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