2013-02-18 59 views
4

我已經子類化了一個單元類,並使用了Storyboard中的原型。所討論的單元格具有嵌入式文本視圖,可以顯示不同長度的文本。 我已經在heightForRowAtIndexPath:中設置了單元格高度,但嘗試調整文本視圖本身的大小cellForRowAtIndexPath:無法正常工作。 它不會在初始加載時調整大小,但是一旦我將單元格從視圖中滾動出來然後返回。但是如果我不斷滾動來回,它偶爾會再次縮小。iOS - 桌面視圖單元格與UITextView沒有正確調整大小

代碼:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    int defaultSize = 0; 
    int height = 0; 
    if(indexPath.section%2==0){ 
     defaultSize = 160; 
     NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section]; 
     NSString *string = [currentPost valueForKey:@"detailsText"]; 
     CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping]; 
     height = stringSize.height + defaultSize; 
     NSLog(@"String Size Before: %f",stringSize.height); 

    }else{ 
     defaultSize = 270; 
     height = defaultSize; 
    } 
    return height; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier; 
    if(indexPath.section%2==0){ 
     CellIdentifier = @"NewsFeedText"; 
    }else{ 
     CellIdentifier = @"NewsFeedImage"; 
    } 
    NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 
    [cell setBackgroundView:[[NewsFeedCellBackground alloc] init]]; 
    [cell.bottomContainer setClipsToBounds:YES]; 
    [cell.bottomContainer.layer setMasksToBounds:YES]; 
    [cell.imagePreview.imageView setContentMode:UIViewContentModeScaleAspectFit]; 
    [cell.bottomContainer setFrame:CGRectMake(0, cell.contentView.frame.size.height-30, cell.contentView.frame.size.width, 30)]; 

    //Data Object 
    NSMutableDictionary *currentPost = [newsFeedPosts objectAtIndex:indexPath.section]; 
    //View 
    cell.userImage.image = [UIImage imageNamed:[currentPost valueForKey:@"userImage"]]; 
    cell.userName.text = [currentPost valueForKey:@"userName"]; 
    cell.createdDate.text = [currentPost valueForKey:@"createdDate"]; 
    cell.patientName.text = [currentPost valueForKey:@"patientName"]; 
    cell.detailsText.text = [currentPost valueForKey:@"detailsText"]; 
    [cell.imagePreview setImage:[UIImage imageNamed:[currentPost valueForKey:@"imagePreview"]] forState:UIControlStateNormal]; 
    [cell.viewCase addTarget:self action:@selector(displayCase:) forControlEvents:UIControlEventTouchUpInside]; 

    //Image 
    [cell.imagePreview addTarget:self action:@selector(displayImage:) forControlEvents:UIControlEventTouchUpInside]; 

    //Data 
    cell.viewCase.tag = [[currentPost valueForKey:@"caseID"] intValue]; 

    //Cell Adjustments 
    NSString *string = [currentPost valueForKey:@"detailsText"]; 
    CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping]; 
    NSLog(@"String Size After: %f",stringSize.height); 
    [cell.detailsText setFrame:CGRectMake(cell.detailsText.frame.origin.x, cell.detailsText.frame.origin.y, cell.detailsText.frame.size.width, stringSize.height+10)]; 
    [cell.detailsText setBackgroundColor:[UIColor redColor]]; 

    return cell; 
} 
+0

您使用的是自動佈局? – dthien 2013-02-18 21:32:20

+1

通過在故事板中添加文本視圖併爲其提供正確的約束,可以更容易完成。它會隨着細胞自動增長,所以你只需要計算細胞的高度。 – rdelmar 2013-02-18 21:52:30

+0

是的,我正在使用autoLayout。儘管如此,仍然有點新。什麼是適當的約束?這個單元格基本上與Facebook News Feed單元格相同(頂部的用戶圖像/名稱,中間的textview,底部的鏈接橫幅視圖) – JimmyJammed 2013-02-18 22:48:28

回答

3

原來使用自動佈局允許我剛纔設置的頂部空間和底部空間的上海華(小區視圖),並且由於細胞高度在heightForRowAtIndexPath改變:的然後textview自動調整大小。

+0

這只是幫助我解決了我的確切問題!非常感謝! – planewalker 2014-04-27 07:47:36

相關問題