2011-01-11 54 views
0
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease]; 
} 
//cell.backgroundView = [[[CustomCell alloc] init] autorelease]; 
cell.selectedBackgroundView = [[[CustomCell alloc] init] autorelease]; 

// At end of function, right before return cell: 
cell.textLabel.backgroundColor = [UIColor clearColor]; 


// Configure the cell. 
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(-30, 3, 300, 22)]; 
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 22, 310, 200)]; 
UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, 300, 20)]; 

Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; 


    myLabel1.text=aBook.title; 
    myLabel2.text=aBook.description; 
    myLabel3.text=aBook.pubDate; 

    NSString *desc = aBook.description; 
    if ([desc isEqualToString:nil]) { 
     NSLog(@"nullll lll "); 
    } 


//myLabel2.textAlignment=UITextAlignmentCenter;    
myLabel2.numberOfLines=5; 
myLabel2.textColor=[UIColor blueColor]; 
myLabel2.lineBreakMode=UILineBreakModeWordWrap; 
myLabel2.font=[UIFont systemFontOfSize:14]; 
myLabel3.numberOfLines=1; 
myLabel1.numberOfLines=1; 
myLabel1.textColor=[UIColor redColor]; 
myLabel1.font=[UIFont systemFontOfSize:20]; 
myLabel1.shadowColor=[UIColor redColor]; 
myLabel1.backgroundColor=[UIColor grayColor]; 
     [cell.contentView addSubview:myLabel1]; 
     [cell.contentView addSubview:myLabel2]; 
     [cell.contentView addSubview:myLabel3]; 

嗨,大家好!我有以下代碼,我想從xml文件中顯示這個。現在它的工作,但靜態行高。 如果xml文件沒有數據,則將其保留爲空白。所以我想根據我提供的數據使用mylabel1,2,3來更改行高。文本動態行高

回答

0

如果我理解正確地問題(和道歉,如果我沒有),那麼:

  1. 落實的tableView:heightForRowAtIndexPath:在您的tableView委託方法 - 得到它的計算並返回一個適當的高度該行;
  2. 配置您添加到contentView的UILabelViews的大小(或僅從超級視圖中移除)。你的tableView這樣做:的cellForRowAtIndexPath:
  3. 確保您重裝該表,或者更好,重裝只有已更改的行 - 這會強制的tableView獲取的高度再次

這裏有一個的tableView的例子:單個節表的heightForRowAtIndexPath方法,根據顯示的段落中有多少文本動態調整單元行的高度。

- (CGFloat) tableView: tableView heightForRowAtIndexPath: indexPath; 
{ 
    CGFloat rowHeight = [self rowHeight:[[self paragraphs] objectAtIndex: [indexPath row]]; 
    return (rowHeight + EI_CELL_MARGIN); 
} 

然後從tableView:cellForRowAtIndexPath:方法中得到一個片段。

NSString* cellText = [[self paragraphs] objectAtIndex: [indexPath row]; 
[[cell paragraphController] setText: cellText]; 
[[cell paragraphController] adjustFrameToTextSize]; 

(如它進入在該代碼中,我子類的UITableViewCell爲了方便。在「paragraphController」僅僅是一個視圖控制器與一個視圖添加到細胞的內容圖。該adjustFrameToTextSize方法重置取決於視圖框要顯示的內容的大小。)

2

按照UITableViewDelegate協議中的規定,您希望在委託中實現tableView:heightForRowAtIndexPath:方法。在這種方法中,你給出的indexPath,這樣你就可以重複

Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];

一旦你的書,你可以檢查其屬性行(貌似titledescriptionpubDate),然後,而不是添加他們到單元格(你稍後做),你返回一個基於屬性值的計算單元格高度。

+0

請讓你的電話一些基本的水平。我是新手到iPhone,我沒有得到你對不起 – Splendid 2011-01-11 18:33:00

1

嗨有一種溶劑嘗試NSString的方法。寫這是單元格的高度

NSString *text1 = [arrOutline1 objectAtIndex:[indexPath row]]; 

CGSize constraint1 = CGSizeMake(CELL_CONTENT_WIDTH , 20000.0f); 
CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap]; 
CGFloat YPostion =MAX(size3.height, 44.0f); 

cellHeight = cellHeight + YPostion + 20;; 

return cellHeight ;