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

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

问题描述

我在 UICell 旁边有两个 UILabel,其中包含动态文本,因此我需要根据内容调整其框架的大小,为此我使用 [stringsizeWithFont] 方法计算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 方法的代码

here is my code for cellRowAtIndexPath method

- (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.

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

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