0

在我的应用程序中,我使用PSCollectionView创建类似于 pinterest 的视图。现在我试图从我的类传递给单元类一个值,我在其中插入我在单元格中设置的 imageView 的高度。当我运行该应用程序时,该应用程序完全使用此高度创建一个单元格,但 imageView 没有尺寸。我在这里发布我的代码:

PSCollectionView 控制器

- (CGFloat)collectionView:(PSCollectionView *)collectionView heightForRowAtIndex:(NSInteger)index {
    NSString *width = [self.arrayWithData[index] objectForKey:@"width"];
    NSString *height = [self.arrayWithData[index] objectForKey:@"height"];
    NSLog(@"%@ e %@", width, height);

    cellHeight = [self getHeightWith:width andHeight:height];

    return cellHeight;    
}

- (CGFloat)getHeightWith:(NSString *)originalWidth andHeight:(NSString *)originalHeight {
    float width = [originalWidth floatValue];
    float height = [originalHeight floatValue];
    float multiplier = height / width;
    // So che la mia cella ha una dimensione massima in larghezza di 100, da questo calcolo l'altezza
    return 100 * multiplier;
}

- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index {
    ProductViewCell *cell = (ProductViewCell *)[self.psView dequeueReusableViewForClass:nil];
    if (!cell) {
        //cell = [[ProductViewCell alloc]initWithFrame:CGRectMake(10, 70, 100, 100)];
        //cell = [[ProductViewCell alloc] initWithFrame:CGRectMake(0,0,collectionView.frame.size.width/2,100)];
        cell = [[ProductViewCell alloc] initWithFrame:CGRectMake(0,0,collectionView.frame.size.width/2,cellHeight + 20)];
    }
    cell.imageHeight = cellHeight;
    cell.labelName.text = [[self.arrayWithData objectAtIndex:index]objectForKey:@"name"];
    NSURL * url = [NSURL URLWithString:[[self.arrayWithData objectAtIndex:index]objectForKey:@"url"]];

    [self loadImageFromWeb:url andImageView:cell.productImage];
    return cell;
}

- (void) loadImageFromWeb:(NSURL *)urlImg andImageView:(UIImageView *)imageView {
    //NSURLRequest* request = [NSURLRequest requestWithURL:url];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:urlImg];

    NSString *authCredentials =@"reply:reply";
    NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authCredentials base64EncodedStringWithWrapWidth:0]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response,
                                               NSData * data,
                                               NSError * error) {
                               if (!error){
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   [imageView setImage:image];
                                   [HUD hide:YES];
                               } else {
                                   NSLog(@"ERRORE: %@", error);
                               }

                           }];
}

这个代码:

ProductViewCell.h

#import "PSCollectionViewCell.h"

@interface ProductViewCell : PSCollectionViewCell {
    float wMargin;
}
@property(nonatomic,strong)UIImageView *productImage;
@property(nonatomic,strong)UILabel *labelName;
// I use this variable to pass the height of the cell from the class who implement PSCollectionView
@property CGFloat imageHeight;

+ (CGFloat)heightForViewWithObject:(id)object inColumnWidth:(CGFloat)cloumnWidth;
@end

ProductViewCell.m

#import "ProductViewCell.h"

#define MARGIN 8.0


@implementation ProductViewCell

- (id)initWithFrame:(CGRect)frame
{
    wMargin = 5.0;
    self = [super initWithFrame:frame];
    if (self) {
//        self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(wMargin, 5, frame.size.width - (wMargin * 2), 125)];
        self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(wMargin, 5, frame.size.width - (wMargin * 2), self.imageHeight)];
        self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(wMargin, 130, frame.size.width - (wMargin * 2), 20)];
        self.labelName.font = [self.labelName.font fontWithSize:12];
        self.labelName.textAlignment = NSTextAlignmentCenter;

        [self addSubview:self.productImage];
        [self addSubview:self.labelName];

        self.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:236.0f/255.0f blue:236.0f/255.0f alpha:1.0];
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 1.0f;
        self.layer.cornerRadius = 10.0f;
        self.layer.borderColor= [[UIColor colorWithRed:207.0f/255.0f green:207.0f/255.0f blue:207.0f/255.0f alpha:1] CGColor];

        [self.productImage setContentMode:UIViewContentModeScaleAspectFit];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
@end

当我尝试记录它的值时,self.imageHeight它在控制台中显示为 0,但您可以看到我将此数据设置为PSCollectionView controller. 如何将我计算的数据发送到我的单元格?有办法吗?

4

2 回答 2

0

根据提供的代码,ProductViewCell 实现中的类属性 imageHeight 仅在初始化新单元格时使用。那时 imageHeight 从未设置或更新,因此它始终为 0。这意味着每当您更新 imageHeight 的值时,单元格属性将被更新,但单元格不会对其进行任何操作。

为此,您可以简单地覆盖 ProductViewCell 中的 setImageHeight: 方法,以便能够触发某些操作。

- (void)setImageHeight:(CGFloat)imageHeight {
    if (_imageHeight != imageHeight) {
        _imageHeight = imageHeight;
        // Do something useful with the new value e.g. calculations
    }
}

每次更新单元格属性时都会调用此方法

cell.imageHeight = ...
于 2013-11-13T10:26:15.773 回答
0

最后我没有使用setter就做到了!解决方案是简单地使用frame.size.height. 我的手机代码如下:

- (id)initWithFrame:(CGRect)frame
{
    wMargin = 5.0;
    self = [super initWithFrame:frame];
    if (self) {
        self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(wMargin, 5, frame.size.width - (wMargin * 2), frame.size.height)];
        self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(wMargin, frame.size.height, frame.size.width - (wMargin * 2), 20)];
        self.labelName.font = [self.labelName.font fontWithSize:12];
        self.labelName.textAlignment = NSTextAlignmentCenter;

        [self addSubview:self.productImage];
        [self addSubview:self.labelName];

        self.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:236.0f/255.0f blue:236.0f/255.0f alpha:1.0];
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 1.0f;
        self.layer.cornerRadius = 10.0f;
        self.layer.borderColor= [[UIColor colorWithRed:207.0f/255.0f green:207.0f/255.0f blue:207.0f/255.0f alpha:1] CGColor];

        [self.productImage setContentMode:UIViewContentModeScaleAspectFit];
    }
    return self;
}

我只是使用 frame.size.height 来定义我的 imageView 的高度,然后你可以看到结果:

具有不同单元格大小的 PSCollectionView 屏幕

于 2013-11-13T11:09:18.313 回答