简体   繁体   English

在cellForRowAtIndexPath中的UITableViewCell的子类中显示UIImageView

[英]Displaying UIImageView in a subclass of UITableViewCell in cellForRowAtIndexPath

I have a property of DotImageView in my UITableViewCell subclass. 我的UITableViewCell子类中有DotImageView的属性。

@property (nonatomic, retain) IBOutlet UIImageView *DotImageView;

I try to display it like this in the cellForRowAtIndexPath method: 我尝试在cellForRowAtIndexPath方法中将其显示为:

static NSString *TargetCellIdentifier = @"TargetDetailTableViewCellIdentifier";
    TargetDetailTableViewCell *cell = (TargetDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:TargetCellIdentifier];
if (cell == nil) {
    UINib *nib = [UINib nibWithNibName:@"TargetDetailTableViewCell" bundle:nil];
    [nib instantiateWithOwner:self options:nil];
    cell = self.TargetCell;
    self.TargetCell = nil;
}
NSUInteger row = [indexPath row];
NSInteger section = [indexPath section];

Target *aTarget = [targetDictionary objectForKey:[NSNumber numberWithInteger:section]];

if (row == 0) {
        UIImage *dot;
        if (aTarget.importance == high) {
            dot = [UIImage imageNamed:@"dot_red.png"];
        }
        else {
            dot = [UIImage imageNamed:@"dot_gray.png"];
        }
        UIImageView *variantImageView = [[UIImageView alloc] initWithImage:dot];
        cell.DotImageView = variantImageView;
        [variantImageView release];
        return cell;

I do not see any image in my cell. 我在手机中看不到任何图像。 The other labels of my subclass of UITableViewCell do get displayed. 我的UITableViewCell子类的其他标签也会显示出来。 Am I missing something with UIImageView? 我是否缺少UIImageView? I'm not super familiar with it. 我对它不是很熟悉。 Thanks. 谢谢。

Insert into TargetDetailTableViewCell Class the method: 将方法插入到TargetDetailTableViewCell类中:

- (void)setDotImage:(UIImage*)dot{



[self.DotImageView setImage:dot];


}

It works for me. 这个对我有用。 I hope it can help you! 希望对您有所帮助!

change your code as 将您的代码更改为

if (row == 0) {
        UIImage *dot;
        if (aTarget.importance == high) {
            dot = [UIImage imageNamed:@"dot_red.png"];
        }
        else {
            dot = [UIImage imageNamed:@"dot_gray.png"];
        }
        cell.DotImageView.image = dot;
        return cell;

what you was doing is you are assigning new UIImageView object to cell.DotImageView now your cell.DotImageView is pointing to this local image view object not to that object which is on xib 您正在做的是将新的UIImageView对象分配给cell.DotImageView现在您的cell.DotImageView指向此本地图像视图对象而不是xib上的该对象

I got it working with 我明白了

[cell.DotImageView setImage:dot];

I'm not sure why it works vs the other method I originally had. 我不确定为什么它与我最初使用的其他方法相比有效。

1 - Basic case 1-基本情况

Simply use the image property of the standard UITableViewCell. 只需使用标准UITableViewCell的image属性。 It is suitable for most cases. 适用于大多数情况。

2 - Your custom case 2-您的自定义案例

Take care, your property is a simple reference to your imageView. 请注意,您的属性是对imageView的简单引用。 So changing its value does not replace the initial one in the subviews tree. 因此,更改其值不会替换子视图树中的初始值。 Instead, keep your imageView and just set its UIImage property would be better. 相反,请保留imageView并仅设置其UIImage属性会更好。

yourImageView.image = aNewImage.

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

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