2014-10-30 63 views
1

您好我使用UICollectionView和UICollectionViewCell我需要一個圖像(圖標)一個標題和一些描述。我是新的IOS開發我想要UICollectionViewCell細胞高度取決於內容,因爲可能是標題和圖像將有特定的高度,但描述有不同的高度。任何人都可以幫助我計算描述標籤的高度。我應該使用textview來描述嗎?動態高度的UICollectionViewCell取決於內容

NSDictionary *service1 = [NSDictionary dictionaryWithObjectsAndKeys: 
       @"Lorem ipsum dolor sit",@"title", 
       @"Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text Lorem ipsum dolor sit is simply dummy text",@"Description", 
       @"image1.png",@"image", 
           nil 
       ]; 
    NSDictionary *service2 = [NSDictionary dictionaryWithObjectsAndKeys: 
       @"Lorem ipsum dolor sit", @"title", 
       @"Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor sit",@"Description", 
       @"image2.png",@"image", 
       nil 
           ]; 
dataarray = [[NSMutableArray alloc] initWithObjects:service1, service2, nil]; 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *identifier = @"Cell"; 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath] ; 
    UIImage * img = [UIImage imageNamed:[[dataarray objectAtIndex:indexPath.row] objectForKey:@"image"]]; 
    UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,70,70)]; 
    iv.image = img; 

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 50, 20)]; 

    [titleLabel setText:[[dataarray objectAtIndex:indexPath.row] objectForKey:@"title"]]; 

    titleLabel.textColor = [UIColor blackColor]; 
    titleLabel.font = [UIFont fontWithName:@"Arial" size:8.5]; 
    titleLabel.backgroundColor = [UIColor clearColor]; 
    titleLabel.textAlignment = NSTextAlignmentLeft; 

    [cell addSubview:titleLabel]; 


    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat screenWidth = screenRect.size.width; 

    UILabel *desclabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, screenWidth-20, 120)]; 

    [desclabel setText:[[dataarray objectAtIndex:indexPath.row] objectForKey:@"Description"]]; 

    titleLabel.textColor = [UIColor blackColor]; 
    titleLabel.font = [UIFont fontWithName:@"Arial" size:8.5]; 
    titleLabel.backgroundColor = [UIColor clearColor]; 
    titleLabel.textAlignment = NSTextAlignmentLeft; 
    [titleLabel sizeToFit]; 

    [cell addSubview:titleLabel]; 
    [cell addSubview:desclabel]; 
    [cell addSubview:iv]; 

    return cell; 

} 

- (CGSize)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 

    // Adjust cell size for orientation 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat screenWidth = screenRect.size.width; 
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) { 

     return CGSizeMake(screenWidth-10, 170.0f); 
    } 
    return CGSizeMake(screenWidth-10, 190.0f); 
} 
+0

http://stackoverflow.com/questions/14255692/dynamically-resizing-a-uicollectionviewcell – Manthan 2014-10-30 10:56:55

+0

@Manthan我有描述文本的問題。我如何檢查描述標籤的動態高度? – 2014-10-30 11:00:38

+0

:請計算您的說明文字的大小並直接傳遞該尺寸。 – Manthan 2014-10-30 11:05:12

回答

1

好的,現在我明白了。您需要動態計算多行UITextView高度並根據它增加默認的UITableViewCell行高。

嘗試使用類似這樣的東西。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath)path 
{ 
    // take the text value 
    NSString *description = service1[@"title"]; 
    // initial rect value 
    CGRect textFrame = CGRectMake(0,0,cellWidth,10000); // width value is enough for this operation 
    UITextView *myTextView = [[UITextView alloc] initWithFrame:textFrame]; // create textview 
    // set text view attributes as multiline, font, font size etc. 
    myTextView.text = description; 
    CGSize size = [myTextView sizeThatFits:myTextView.frame.size]; // that's all 
    CGFloat height = size.height + someDefaultCellHeight; 
    return height; 
} 

您可以在-(void)viewDidLoad方法創造一些私人UITextView成員不是每次創建它的heightForRow委託方法調用。

+0

不,我需要全寬盒子。不是網格。 – 2014-10-30 11:08:15

+1

爲什麼不使用UITableView而不是UICollectionView?用 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath)路徑委託方法。 – Astoria 2014-10-30 11:10:09

+0

好的主意。我會現在嘗試。但可能會出現再次需要計算尺寸的說明 – 2014-10-30 11:12:04

相關問題