2012-01-17 145 views
1

我想让我的单元格根据单元格中文本的大小动态地改变高度..目前我有文字包装..但只要有很多内容的单元格(如果它进入第三行),你看不到第二行的任何内容。动态UITableViewCell高度

这是我到目前为止..希望有人可以看到,如果我失去了一些东西或做错了什么...任何帮助将不胜感激。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UILabel *label = nil; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     label = [[UILabel alloc] initWithFrame:CGRectZero]; 
     [label setLineBreakMode:UILineBreakModeWordWrap]; 
     [label setMinimumFontSize:FONT_SIZE]; 
     [label setNumberOfLines:0]; 
     [label setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 
     [label setTag:1]; 

     [[cell contentView] addSubview:label]; 
    } 

    // //Display cells with data that has been sorted from startSortingTheArray 
NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; 
NSString *key = [keys objectAtIndex:indexPath.row]; 

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

if (!label) 
    label = (UILabel*)[cell viewWithTag:1]; 

[label setText:key]; 
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; 






//Applise current key value to cell text label 
//cell.textLabel.text = key; 
return cell; 
} 

//Cell size for word wrapping. 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 

    //Display cells with data that has been sorted from startSortingTheArray 
    NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; 
    NSString *key = [keys objectAtIndex:indexPath.row]; 

    NSLog(@"%@", key); 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 44.0f); 

    return height + (CELL_CONTENT_MARGIN * 2); 
} 

更新上面的代码,现在是我的工作版本..反正:)

+0

这听起来像问题不是单元格的高度,而是标签的高度。那是对的吗?这是有道理的,因为我没有看到你在任何地方设置标签的高度。 – sosborn 2012-01-18 00:05:48

+0

hrmm ...以及我想这可能是..我会研究整理标签,我想我已经找到了一个体面的教程..我会给它现在... – 2012-01-18 00:08:59

+0

我添加了UILabel,并设法让单元格的大小大约加倍..但是它发生在tableview中的每个单元格上,而不仅仅是具有更多我想要动态放大的文本的单元格。 – 2012-01-18 00:26:53

回答

3

重新开始,并按照单词本教程词,你会被罚款: Cocoa is My Girlfriend

它实际上你看起来像是看了本教程,只看到了一部分内容。我仍然没有看到你设置标签的框架。

+0

是的,这就是我一直在用的..它现在有一个问题,我不知道该怎么办... ** cell = [[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@“ Cell“] autorelease]; **不包括autorelease如果你使用ARC initWithFrame:reuseIdentifier:已经停止使用,所以我不知道该怎么做... – 2012-01-18 00:54:18

+0

好吧得到它的工作..似乎在搞乱我的单元格选择和附件的意见,但生病试图找出自己的问题..感谢您的帮助 – 2012-01-18 01:04:23

相关问题