0

有一个自定义单元格。里面有两个标签。其中一个名为 nameLabel 的高度必须动态变化。有时可以是 1,2 或 3 行。但是这些行是相互的,它们穿过自己的行线。我怎么解决这个问题?

标签和自定义单元格对象的使用自动布局选项被禁用。标签高度必须动态改变,然后是CustomCell。然后是 tableRow。我的头很混乱。为什么我看不到 CustomCell 的背景颜色正在改变?

谢谢

这是我的代码:

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0; // allows label to have as many lines as needed
    label.text = [[self.dataList objectAtIndex:indexPath.row] objectForKey:@"NAME"];
    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(320, 63) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat h = labelSize.height;
    NSInteger x=0.0;
    if (h==63.0) x=30;
    if (h==42.0) x=20;
    if (h==21.0) x=10;

    return h+30;
}

- (UITableViewCell*)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellid";

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];    
    if (cell == nil)
    {
        cell = (CustomCell*)[[[NSBundle mainBundle]
                              loadNibNamed:@"CustomCell" owner:nil options:nil]
                             lastObject];
    }    
    // customization
    NSDictionary *d = [self.dataList objectAtIndex:indexPath.row];

    cell.nameLabel.text = [d objectForKey:@"NAME"];
    cell.cityLabel.text = [d objectForKey:@"CODE"];
    cell.indexPath = indexPath;    

    return cell;
}

在此链接上有自定义单元的模拟器和 xib 的图片,可以轻松理解问题: http ://compfreek.wordpress.com/2013/08/14/custom-cell-for-table-row-height-changes-动态的/

4

1 回答 1

0

听听是另一种方法,根据您的代码尝试此更改,它可能会有所不同,但它可能会解决您的问题。我仅通过代码放置所有视图,因为这对我来说很容易检查出来。

  //in your subclassed class
  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {
    // Initialization code

    //may be u did same i am adding the label to cell
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    nameLabel.numberOfLines = 5; //set number of lines 
    nameLabel.tag = 100;
    [self addSubview:nameLabel];

    UILabel *otherLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    otherLabel.tag = 200;
    [self addSubview:otherLabel];

   }
  return self;

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
   [super setSelected:selected animated:animated];
   // Configure the view for the selected state
   //do your customisation when cell is selected
}

-(void)layoutSubviews
{
   //in this method i am setting the frames for label 

   [super layoutSubviews];
   UILabel *nameLabel = (UILabel *) [self viewWithTag:100];
   nameLabel.text = self.nameString;


  CGSize maxSize = CGSizeMake(self.bounds.size.width / 2, MAXFLOAT);//set max height
  CGSize nameSize = [self.nameString sizeWithFont:[UIFont systemFontOfSize:17]
                       constrainedToSize:maxSize
                           lineBreakMode:NSLineBreakByWordWrapping];
  nameLabel.frame = CGRectMake(self.bounds.origin.x +2,self.bounds.origin.y+3, nameSize.width, nameSize.height);

  UILabel *otherLabel = (UILabel *) [self viewWithTag:200];
  otherLabel.frame = CGRectMake(nameLabel.frame.size.width+15,self.bounds.origin.y+3, 100, 40);
  otherLabel.text = self.otherString;

}

 //in your main class

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
  if(cell == nil)
   {
      cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"]autorelease];
   }

   //change it for ur need
   NSString *name = @"hear is your long length name uzamaki naruto from uzamaki clan";
   NSString *other = @"other name";
   cell.nameString = name;
   cell.otherString = other;

   return cell;
  }

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   NSString *name = @"hear is your long length name uzamaki naruto from uzamaki clan";


  //replace it your height logic 
  float cellWidth = 350; // ur cell width 
  CGSize maxSize = CGSizeMake( cellWidth / 2, MAXFLOAT);//set max height
  CGSize nameSize = [name sizeWithFont:[UIFont systemFontOfSize:17]
                              constrainedToSize:maxSize
                                  lineBreakMode:NSLineBreakByWordWrapping];

 return nameSize.height + 10;// for ur height

}


于 2013-08-14T11:47:52.380 回答