尝试根据单元格子视图的文本量计算UILabel的帧大小



下面的代码确定了UILabel的帧大小,我认为它确实有效,但是当我将它放在我的rowAtIndexPath中用于UItable时,我得到了不稳定的结果。

也许,我不完全理解reuseIdentifier是如何或做什么的,但是我只在单元格为nil时才放置代码来计算帧。实际情况是,高度只计算前三个单元格,然后按顺序重复计算其余的单元格。例如,单元格1的高度用于单元格4的高度。

也许有人能给我指出正确的方向,告诉我应该如何设置我的计算。

谢谢!

if(cell == nil){
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                reuseIdentifier:DisclosureButtonCellIdentifier] autorelease];
    //start adding custom subviews to the table cell

    //addSubview for Description
UILabel *descValue = [[UILabel alloc] init];
    NSString *descString = rowData.summary;
    CGSize maximumSize = CGSizeMake(185, 130);
    UIFont *descFont = [UIFont fontWithName:@"HelveticaNeue" size:12];
    CGSize descStringSize = [descString sizeWithFont:descFont 
                                   constrainedToSize:maximumSize 
                                       lineBreakMode:descValue.lineBreakMode];
    CGRect descFrame = CGRectMake(125, 60, 185, descStringSize.height);
    descValue.frame = descFrame;
    descValue.backgroundColor = [UIColor redColor];
    descValue.font = descFont;
    descValue.tag = kDescriptionValueTag;
    descValue.lineBreakMode = UILineBreakModeWordWrap;
    descValue.numberOfLines = 0;
    [cell.contentView addSubview:descValue];
    [descValue release];
}

UILabel *desc = (UILabel *)[cell.contentView viewWithTag:kDescriptionValueTag];
    desc.text = rowData.summary;

使用NSString UIKit Additions,你可以使用特定的字体获得字符串的宽度:

[myString sizeWithFont:myFont];

在这个add集合中还有其他几个函数,可以帮助计算出一段文本的大小,使用不同的布局选项,单行和多行文本等。

reuseIdentifier的目的是让你重用一个单元格——在特定的地方有特定的子视图——而不需要设备花费执行时间来做所有的布局。在iPhone 1的时候肯定更有用,当时处理器要慢得多。在您更熟悉reuseIdentifiers之前,我建议您在每次调用该函数时创建一个新单元格。

每次操作系统调用cellForRowAtIndexPath时,您都需要正确填写内容。任何需要根据行调整大小或更改的内容都应该设置。

坦率地说,我并没有试图完全理解你的代码。这几乎是不可能的,特别是因为你用括号关闭方法而不返回任何东西,但我认为你指的是cellForRowAtIndexPath,需要返回UITableViewCell对象。

显然我们只看到了代码的一部分。

然而,正确布局单元格并不是完整的任务。您需要告诉表格每个单元格的高度。您可能想要实现

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

用于该任务。如果单元格高度变化,这种方法很好。如果单元格的高度不同于标准,但单元格之间没有变化,那么设置tableView的rowHeight属性就足够了。

我认为在您的情况下,heightForRowAtIndexPath方法的实施是强制性的。

顺便说一句,你可能想看看uitableellview的子类,并在你的子类中实现layoutSubviews方法。

您没有显示所有的代码,但通常要实现tableView:cellForRowAtIndexPath:您从以下开始:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // get a cell from the queue of reusable cells, if any
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: DisclosureButtonCellIdentifier];
    if (cell == nil) {
        // No cell to reuse, need to create a new one
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier:DisclosureButtonCellIdentifier] autorelease];  
        // add cell subviews here      
    }
    // fill in cell content and resize subviews here
    ...
    return cell;
}

一旦你已经创建了3(在你的情况下)单元格,他们被重用为表滚动,所以你不必继续创建单元格。dequeueReusableCellWithIdentifier的调用将返回一个先前创建的单元格,如果它不再被使用(它从顶部或底部滚动)。

最新更新