2011-01-11 59 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 ;