【问题标题】:UIlabel gets shrink on scrolling UiTableViewUIlabel 在滚动 UiTableView 时缩小
【发布时间】:2012-09-17 23:56:23
【问题描述】:

我在UICell 旁边有两个UILabel,其中包含动态文本,因此我需要根据内容调整其框架的大小,为此我使用[string sizeWithFont] 方法来计算标签视图框架在@ 内的高度987654324@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] ;

}

【问题讨论】:

    标签: iphone uitableview ios5 uilabel


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      检查链接:

      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;
      }
      

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

      【讨论】:

        【解决方案3】:
        NSString *reuseIdentifier = @"EventTitleCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        cell=nil;
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ;
        
        }
        

        【讨论】:

          【解决方案4】:

          试试这个代码。

              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

          【讨论】:

            猜你喜欢
            • 2018-01-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-05
            • 1970-01-01
            • 2013-06-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多