2012-11-14 23 views
2

我有一個UITableView與4 UILabel's:標題,正文,作者和日期,他看起來是這樣的:根據的UILabel規模擴大的UITableViewCell

cell55

我想做到的是,當用戶點擊細胞本身,另一個標籤應該被添加到細胞中,「身體」標籤和細胞應該根據這個標籤尺寸擴大。

事情是這樣的:

cellblsize

我怎麼能這樣做?我搜索了stackoverflow,嘗試了一些代碼片斷,但仍未找到正確的解決方案。

謝謝!

編輯1:12年11月14日14:52在

我設法與當前的文本改變的UILabel的大小:

- (CGRect)resizeLabelByFontSize:(UILabel *)customCellLabel withMaxHeightSize:(CGFloat)maxHeight 
{ 
    CGSize maximumLabelSize = CGSizeMake(239, maxHeight); 

    CGSize expectedLabelSize = [customCellLabel.text sizeWithFont:customCellLabel.font constrainedToSize:maximumLabelSize lineBreakMode:customCellLabel.lineBreakMode]; 

    //adjust the label the the new height. 
    CGRect newFrame = customCellLabel.frame; 
    newFrame.size.height = expectedLabelSize.height; 

    return newFrame; 
} 

但據我如何可以改變細胞的大小到新的UILabel的大小?

+0

你有沒有試過這個鏈接,在我的答案中提到.. !!! – Kamarshad

回答

0

所以,爲了做到這一點,使用消耗的UITableViewCell,我已經創建了2個不同的定製單元,在啓動表顯示第一個單元格,當我單擊單元格時,表格顯示第二個單元格。這很容易 - 是的!

所以我有UIViewController的使用實現表委託方法的UITableView:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if([self.selectedCellIndexPath isEqual:indexPath]) 
    { 
     return [self expandedCellHeight:indexPath]; 
    } 
    else 
    { 
     return kRegularCellHeight; 
    } 
} 

-(CGFloat)expandedCellHeight:(NSIndexPath *)indexPath 
{ 
    CGSize maxSize = CGSizeMake(303, 200); 
    NSString* bodyText = [[self.data objectAtIndex:indexPath.row] objectForKey:kForumMessagesBody]; 
    CGSize fitSize = [bodyText sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = 384 - 69 + fitSize.height; 
    NSLog(@"expandedHeight: %f",height); 
    return height; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Answer cell 
    if ([self.selectedCellIndexPath isEqual:indexPath]) 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:[ForumCell expandedAnswerReuseIdentifier]]; 
     if (cell == nil) 
     { 
      cell = [ForumCell expandedAnswerCell]; 
     } 
     self.expandedCell = cell; 

    } 
    else 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:[ForumCell reqularAnswerReuseIdentifier]]; 
     if (cell == nil) 
     { 
      cell = [ForumCell regularAnswerCell]; 
     } 
    } 

    cell.labelMedia.text = [self.data objectAtIndex:indexPath.row]; 

    return cell; 
} 

我也有自定義單元格,稱爲類ForumCell.hForumCell.m,它有2個不同的XIB文件:ForumRegularAnswerCell.xibForumExpandedAnswerCell.xib,我有以下代碼中ForumCell.h

+ (NSString*)reqularAnswerReuseIdentifier 
{ 
    return @"RegularAnswerCellReuseIdentifier"; 
} 

+ (NSString*)expandedAnswerReuseIdentifier 
{ 
    return @"ExpandedAnswerCellReuseIdentifier"; 
} 

+ (ForumCell*)regularAnswerCell 
{ 
    NSArray* objs = [[NSBundle mainBundle] loadNibNamed:@"ForumRegularAnswerCell" owner:self options:nil]; 
    ForumCell* result = [objs objectAtIndex:0]; 

    return result; 
} 

+ (ForumCell*)expandedAnswerCell 
{ 
    NSArray* objs = [[NSBundle mainBundle] loadNibNamed:@"ForumExpandedAnswerCell" owner:self options:nil]; 
    ForumCell* result = [objs objectAtIndex:0]; 

    return result; 
} 

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    self = [super initWithCoder:decoder]; 
    if (self) 
    { 
     _originalCellHeight = self.frame.size.height; 
     _originalLblBodyHeight = self.lblBody.frame.size.height; 
    } 
    return self; 
} 

您也可以使用超過2個xibs,如果你想它取決於你。但這是基礎知識。

享受!

0

實現以下方法

– (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    // cast cell, add label, expand labels etc 

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [indexPath isEqualTo:[tableView indexPathForSelectedRow]] ? /* expanded height */ : 80 /* normal height */; 
} 

如果想要另一個行被選中後,即使再定製BOOL屬性添加到您的自定義單元格,例如留選擇的行expanded,並用它來確定高度。

0

可以使用的tableView:didSelectRowAtIndexPath方法

在該方法中,你可以創建你的代碼取消隱藏身體的標籤,調整一切的相對位置。計算行的新高度,然後調用表視圖的reloadRowsAtIndexPath:withRowAnimation:方法。

對不起,如果沒有很多的細節,但希望這應該讓你在正確的軌道上。

0

好吧,首先...要擴大你需要的東西是這樣的:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 

現在這裏是漁獲:

  • 您應該計算的UITableViewCell的大小(擴大和非擴展)
  • 這樣做當你實際上滾動可能會很昂貴,並會給你一個不好的經驗

我的建議是:

  • 計算兩側,在你實際上已經得出結論:打造UITableView,因爲你想擁有動態的大小。如果你不這樣做,並且所有的單元格都會擴大相同的大小,你可以使用lammmert所說的。
1

由有關

這裏看到的圖片是剛剛創建的動態幀的UILabel方法看看這個 通過獲得對UIlabel高度和寬度可以計算整個高度,可以設置這樣的描述標籤上顯示UITableView.

- (void)setLabeltextWithVerticalAlignTop:(NSString *)theText 
{ 
CGSize labelSize; 
// here labelSize is hard-wired but could use constants to populate the size 

labelSize = CGSizeMake(210, 129);//this is just for example 
//now create the Size from textString SO that We could assign this size to the Label. 

CGSize theStringSize = [theText sizeWithFont:lblTitle.font constrainedToSize:labelSize lineBreakMode:lblTitle.lineBreakMode]; 
lblTitle.frame = CGRectMake(lblTitle.frame.origin.x, lblTitle.frame.origin.y, theStringSize.width, theStringSize.height); 
lblTitle.text = theText; 

} 

呼叫上述方法的行高設置,您需要通過文字描述標籤的高度和寬度。 當您獲得該標籤的高度時,現在基於此您可以調整TableView的高度。

編輯:上面的代碼就創建動態幀爲的UILabel

You should take a view of this這是你在尋找什麼......!在這裏,你會發現一個示例代碼太。

編輯:當你編輯你的問題看,它只是你需要將它轉換成可運行代碼的邏輯它在這裏。

在代碼中使用下面的方法調用每一行,並在其中進行一些計算。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CGFloat rowHeight=0.0; 
//here it seems cell have 4 subview added on it. 
//so if you could calculate the totla hieht of them. 

//so what you really need to do.you just use hieght calculative Method for getting hieght of each of three UILabel 
//you need to modify `setLabeltextWithVerticalAlignTop` method . 
rowHeight= [self setLabeltextWithVerticalAlignTop:@"pass the correspondingText"];// suppose it returns some hieght for FisrtLabel. 

//suppoose here you get the 20.0 height here 

rowHeight= rowHeight+[self setLabeltextWithVerticalAlignTop:@"pass the correspondingText"]; 

//假設它爲secondUIlabel返回一些高度。

//suppoose here you get the 40.0 height here 

rowHeight= rowHeight+ [self setLabeltextWithVerticalAlignTop:@"pass the correspondingText"]; 

// suppose it returns some hieght for ThirdUIlabel. 
// suppoose here you get the 15.0 height here 
//here you have totla height you just need to add some gapping floating value for all of three UIlabel.so that the could not overlap like as. 

rowHeight= rowHeight+20.0; 

    //now you can return that total height 
    return rowHeight; 
} 

注:這只是你需要把它轉換成可運行code.i相信這能幫助邏輯。

我希望它可以幫助你。

0

NSIndexPath *selectedRow;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
selectedRow = indexPath; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
if(indexPath == selectedRow){ 
    //return your custom value 
} 
return 100; 

} 

我認爲它看起來就像是

相關問題