繁体   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