简体   繁体   English

具有UIImageView的自定义UITableViewCell不为某些单元格加载图像

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

I have this really bizarre problem. 我有这个非常奇怪的问题。

Basically I have a UITableViewCell subclass that ive written and linked some outlets to a Nib. 基本上,我有一个UITableViewCell子类,该子类通过ive编写并将某些插座链接到Nib。

subclass of Cell.h Cell.h的子类

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

subclass of Cell.m 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;
} 

In the view controller which manages the table view for the cell, 在管理单元格表视图的视图控制器中,

I have registered with the Nib using the following code: 我已经使用以下代码向Nib注册:

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

In cellForRowAtIndexPath, I dequeue the cell and set the image... 在cellForRowAtIndexPath中,我将单元出队并设置图像...

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell];

switch (indexPath.section) {


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

    default:
        break;
}

return cell;

} }

The strange thing is, say I have 10 cells in a section 2 (as above), and I set them up in an identical way, some cells show the cogwheel icon, and some don't! 奇怪的是,假设我在第2节中有10个单元格(如上所述),并且以相同的方式设置它们,有些单元格显示了齿轮图标,有些则没有! Scrolling those cells in and out of view seems to change which cells show the icon and which don't. 将这些单元格滚动进和移出视图似乎可以更改哪些单元格显示图标,哪些不显示图标。

This means that the registering the cell, including setting the re-use identifier, setting the subclass in the nib file, all of those sorts of things are fine (the common pitfalls). 这意味着注册单元格,包括设置重用标识符,设置nib文件中的子类,所有这些事情都很好(常见的陷阱)。

This pattern of behaviour seems to me to be memory related - as if something is being deallocated somewhere... 在我看来,这种行为模式与内存有关-好像某个东西被释放了。

Here is a picture to illustrate what I mean: 这是一张图片来说明我的意思:

在此处输入图片说明

Please help! 请帮忙!

Thanks 谢谢

Vb Vb

Edit: 编辑:

OK just to be clear, im not using any data yet with this table, but in spite of that there's no reason why I those cogwheels shouldn't be appearing! 好的,只是要明确一点,我尚未在此表中使用任何数据,但是尽管如此,我没有理由不应该出现这些齿轮!

- (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;
}

The output of that is... 的输出是...

在此处输入图片说明

My point is that if im setting the same image for all cells which are in the 2nd section 'settings', why do only some of them have the image showing? 我的观点是,如果我为第二部分“设置”中的所有单元格设置了相同的图像,为什么只显示其中一些图像?

The Custom Cell class: 自定义单元格类:

@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

The cells have custom fonts, an arrow view, a background view - they all work as they should! 单元具有自定义字体,箭头视图,背景视图-它们都可以正常工作! They change depending on section. 它们根据部分而变化。 Its just the image that doesn't work. 它只是图像不起作用。 You can see that this function called by cellForRowAtIndexPath will either set an image, if its provided, or set the imageview to nil if it isn't. 您可以看到由cellForRowAtIndexPath调用的此函数将设置图像(如果提供),或者将图像视图设置为nil(如果未提供)。

Turns out that the problem was related to using 'imageView' as the name of my imageView in my nib. 原来,问题与在笔尖中使用“ imageView”作为imageView的名称有关。 This was later changed but the original relationship was to the instance variable of UITableViewCell rather than Custom Cell. 后来进行了更改,但原始关系是UITableViewCell的实例变量而不是Custom Cell。

Moral of the story: Don't call the image view of your custom table view cell 'imageView' ! 故事的寓意:不要将自定义表格视图单元格的图像视图称为“ imageView”!

UITableViewCell s are redrawn from old cells using dequeueReusableCellWithIdentifier: . 使用dequeueReusableCellWithIdentifier:从旧单元格重绘UITableViewCell If you do not want the cells being reused, then you will need to initialize a new cell every time in your tableView:cellForRowAtIndexPath: function. 如果您不想重复使用这些单元格,则每次在tableView:cellForRowAtIndexPath:函数中都需要初始化一个新单元格。

However, if you want to reuse the cells (recommended), then you should be calling your - (void) setUpCell... image:(UIImage*)image function every time inside of your tableView:cellForRowAtIndexPath: in order to ensure that the image is set to only the correct cells. 但是,如果要重用单元格(推荐),则应该每次在tableView:cellForRowAtIndexPath:内部调用- (void) setUpCell... image:(UIImage*)image函数,以确保图像仅设置为正确的单元格。

You also seem to be using data stored inside your UITableViewCell subclass, but when the cell is reused on a different row, it will still think that it is the same cell from its original row. 您似乎还使用了存储在UITableViewCell子类中的数据,但是当单元格在另一行上重用时,它仍会认为它与原始行是同一单元格。

Ex: A cell is created in row 2 and has an icon image. 例如:在第2行中创建一个单元格,并具有图标图像。 Cell 2 is off the screen and is now being used for row 16. Row 16 shouldn't have an image, but the cell still believes it is the same cell from row 2 and continues to show its image. 单元2不在屏幕上,现在用于第16行。第16行不应有图像,但是该单元仍然认为它是第2行中的同一单元,并继续显示其图像。

If you would like to have each cell be reused as well as have unique data for each one, only use an exterior data set (ex: an NSArray ) that provides the information necessary to draw the cell and pass that information to the cell each time, rather than depending on the information you give the cell upon its creation. 如果您想重用每个单元格并为每个单元格提供唯一的数据,请仅使用外部数据集(例如NSArray ),该数据集提供绘制单元格并将信息每次传递给单元格所需的信息。 ,而不是根据单元创建时提供的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM