質問

There is a custom cell. In it there is two labels. One of them called nameLabel's height has to change dynamically. It can be 1,2 or 3 lines sometimes. But the rows are on eachother, they cross their own row lines. How can I solve this problem?

The labels and Custom Cell object's Use Autolayout option is disabled. The label height has to change dynamic, And then the CustomCell's. And then tableRow. My head is confused. And why can't I see CustomCell's background color is changing?

Thanks

Here is my code for :

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

At this link there is picture of simulator and xib of the Custom Cell to understand easy the problem: http://compfreek.wordpress.com/2013/08/14/custom-cell-for-table-row-height-changes-dynamic/

役に立ちましたか?

解決

Hear is another way try this change according to ur code it may different, but it may solve your problem.I am putting all views through code only, because it is easy for me check this out.

  //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

}


ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top