0

在我的应用程序中,我使用的是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;
}

但结果是这样的:

模拟器视图

您可以看到文本和图像不在中心,我怎样才能使它们居中?请注意,图像可能有不同的大小。你能帮我找到纠正观点的方法吗?

更新:这是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);
                               }

                           }];
}
4

1 回答 1

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)];
于 2013-11-12T11:27:28.510 回答