5

我在里面有两个UILabelUICell其中包含动态文本,所以我需要根据内容调整其框架的大小,为此我使用[string sizeWithFont]方法来计算标签视图框架内部TableView heightForRowAtIndexPath的高度,以根据标签高度设置单元格的高度。现在问题是当我滚动表格时,如果我删除[label sizeToFit]方法它不会缩小,单元格内的标签会开始缩小,但我的标签会重叠,看起来很乱。我哪里错了请指导我..

这是我的cellRowAtIndexPath方法代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cell");
BOOL ente = FALSE;

static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (CustomCell *) currentObject;
            ente = TRUE;
            break;
        }
    }
}
/*
[cell.title sizeToFit];
[cell.dataTime sizeToFit];

CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

cell.title.frame = CGRectMake(50, 6, size1.width, size1.height);


CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

cell.dataTime.frame = CGRectMake(50, 24, size2.width, size2.height);

*/
cell.title.text = [mainEventArray objectAtIndex:[indexPath row]];
cell.dataTime.text = [mainEventTimeArray objectAtIndex:[indexPath row]];



//if (ente) {
  //  NSLog(@"helllloo");

    [cell.title sizeToFit];
    [cell.dataTime sizeToFit];
//}


return cell;
}

heightForRowAtIndexPath方法

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


static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

{

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }
}

    CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

    cell.title.frame = CGRectMake(0, 0, size1.width, size1.height);


CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

cell.dataTime.frame = CGRectMake(0, 0, size2.width, size2.height);


//NSLog(@"%f", size1.height + size2.height + 20);

    return size1.height + size2.height + 20;

//NSString *str = [heights_ objectAtIndex:[indexPath row]];

  // return [str floatValue] ;

}
4

4 回答 4

3

而不是进行自己的定制。尝试使用动态 uitableviewcell 高度。你会发现真的比你所做的更有用且相对容易。

您可以在此处查看:http ://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ 并附有详细说明。享受编程。

于 2012-09-26T11:22:00.953 回答
0

在您的情况下,您需要根据标签设置单元格的高度。

检查链接:

http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/

有一个函数名为

-(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject

使用该函数计算高度为:

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat _height  = [self getHeightForLabel:self.label.text font:[self.label font]];
    return _height;
}

并在单元格中创建和添加标签时执行相同的操作。我希望这将有所帮助。

于 2013-05-22T07:37:11.723 回答
0
NSString *reuseIdentifier = @"EventTitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
cell=nil;
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ;

}
于 2013-09-20T08:32:21.230 回答
-1

试试这个代码。

    label.numberOfLines = 0; // allows label to have as many lines as needed
    label.text = @"some long text";
    [label sizeToFit];
    NSLog(@"Label's frame is: %@", NSStringFromCGRect(label.frame));

供参考:点击这里

于 2012-09-26T11:18:26.343 回答