使用PSCollectionView中心UIImageView

时间:2013-11-12 11:10:51

标签: ios objective-c uiimageview

在我的应用中,我正在使用PSCollectionView,我想在其中显示文字标签和图片。我对如何居中图像和文字感到疯狂。我试着用这种方式:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 75)];
        self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 100, 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];
    }
    return self;
}

但结果如下:

Simulator view

您可以看到文字和图片不在中心,我如何将它们居中?请注意,图像可能具有不同的大小。 你能帮我找到纠正观点的方法吗?

更新: 这是cellForRowMethod以及下载图片的方法:

- (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.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 =@"user:password";
    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];
                               } else {
                                   NSLog(@"ERRORE: %@", error);
                               }

                           }];
}

1 个答案:

答案 0 :(得分:0)

你正在使用框架,所以我假设你从代码(而不是故事板)做所有事情,所以:

self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 75)];
self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 100, 20)];

此处您有不同的x值。这可能会导致中心问题。

您应该做的是读取单元格框架,然后使用它来设置内容框架。所以你应该从方法中调用它:

- (MyCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index
{
    MyCell *cell = (MyCell *)[collectionView dequeueReusableViewForClass:[MyCell class]];
    if (cell == nil) {
        cell = [[MyCell alloc] initWithFrame:CGRectMake(0,0,collectionView.frame.size.width,50)];
    }
   // here you have to do other stuff, like assigning display text or something like that.
}

这将创建高度为50的单行单元格。如果你想要两行,你应该实现计数器并使用'x'值进行操作。

我真的不知道PSCollectionView。你为什么需要它? UICollectionView非常好。

修改

太棒了,所以你只需要更改单元格类initWithFrame中的这一行: float wMargin = 5;

self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(wMargin, 5, frame.size.width-(wMargin*2), 75)];
self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(wMargin, 80, frame.size.width-(wMargin*2), 20)];

如果您想从边缘移动内容,则应使用wMargin变量

进行操作

<强> EDIT2: 嗯奇怪。尝试在cellForRowAtIndex

中更改此行
cell = [[ProductViewCell alloc]initWithFrame:CGRectMake(10, 70, 100, 100)];

为:

cell = [[ProductViewCell alloc] initWithFrame:CGRectMake(0,0,collectionView.frame.size.width/2,100)];