动态UITableViewCell高度 [英] dynamic UITableViewCell height

查看:102
本文介绍了动态UITableViewCell高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的单元格根据单元格中的文本数量动态地改变高度。目前我有单词包装..但是一旦单元格有很多内容(如果它去了第3行)你看不到第二行以外的任何东西。



这是我到目前为止所做的...希望有人能看出我是否遗漏了某些东西或做错了什么...任何帮助将不胜感激。

   - (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];
}

// //显示已经从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];
[标签setFrame:CGRectMake(CELL_CONTENT_MARGIN,CELL_CONTENT_MARGIN,CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2),MAX(size.height,44.0f))];






//将当前键值应用于单元格文本标签
//cell.textLabel.text =键;
返回单元格;
}

//自动换行的单元格大小。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{

//显示已从startSortingTheArray
NSArray * keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]排序的数据的单元格;
NSString * key = [keys objectAtIndex:indexPath.row];

NSLog(@%@,密钥);

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);

返回高度+(CELL_CONTENT_MARGIN * 2);
}

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

解决方案

重新开始并逐字逐句地学习本教程,你会没事的:
可可是我的女朋友



<实际上看起来你看过这个教程并且只是遵循了它的一部分。我仍然没有看到你设置标签的框架。


I am trying to get my cell to dynamicly change height depending on how much text is in the cell.. Currently I have the words wrapping.. but as soon as there is to much content for the cell (if it goes to a 3rd line) you cannot see anything past the 2nd line.

This is what I have so far.. hopefully someone can see if I am missing something or doing something wrong... any help would be appreciated.

 - (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);
}

Update the code above is now a working version.. for me anyway :)

解决方案

Start over and follow this tutorial word for word and you'll be fine: Cocoa is My Girlfriend

It actually looks like you looked at this tutorial and only followed part of it. I still don't see you setting the frame of the label though.

这篇关于动态UITableViewCell高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆