滚动UiTableView时UIlabel会缩小

时间:2012-09-26 11:07:54

标签: iphone uitableview ios5 uilabel

我在UILabel一侧有两个UICell,其中包含动态文字,所以我需要根据内容调整其框架大小,我使用[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 个答案:

答案 0 :(得分:3)

而不是自己定制。尝试使用动态uitableviewcell高度。你会发现非常有用的&比你做的更容易。

您可以在此处查看:http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/,其中包含大量说明。享受编程。

答案 1 :(得分: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;
}

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

答案 2 :(得分:0)

NSString *reuseIdentifier = @"EventTitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
cell=nil;
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ;

}

答案 3 :(得分:-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));

供参考:click here

相关问题