滚动UiTableView时UIlabel会缩小 [英] UIlabel gets shrink on scrolling UiTableView

查看:83
本文介绍了滚动UiTableView时UIlabel会缩小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two UILabel in side of UICell, which contains dynamic text so i need to resize its frame according to content, for which im using [string sizeWithFont] method to calculate the height of label view's frame inside of TableView heightForRowAtIndexPath to set the height of cell according to label height. Now what the problem is when i scroll my table, label inside of cell starts to shrink if i remove [label sizeToFit] method it doesn't shrink but from that my labels are getting overlapped which looks very messy. Where i am wrong please guide me..

这是我的代码 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] ;

}


推荐答案

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

Instead of doing your own customization. Try with dynamic uitableviewcell height. You will find really usefull & comparatively easier than what you have done.

您可以在此处查看: http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ 充满解释。享受编程。

You can check it here : http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ with full of explanation. Enjoy programming.

这篇关于滚动UiTableView时UIlabel会缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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