Changing height dynamic for Table Rows , CustomCell and Layout

e.ozmen

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/

Shankar BS

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

}


Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Highcharts dynamic height with table layout

From Dev

Android: How to equally divide the table layout height to table rows in programmatically

From Dev

Dynamic height of table is not set

From Dev

Changing columns to rows layout with Bootstrap

From Dev

Changing columns to rows layout with Bootstrap

From Dev

Changing the css of table rows?

From Dev

OnClickListener on dynamic table layout

From Java

Is it possible to obtain a dynamic table view section header height using Auto Layout?

From Dev

Dynamic table rows and columns

From Dev

JQuery and Dynamic Table rows

From Dev

Dynamic Rows and Table

From Dev

React dynamic table rows

From Dev

Changing Height of Relative Layout with same speed as user Finger Moves on Layout

From Dev

Table with dynamic row height issue

From Dev

Table with dynamic row height issue

From Java

Height equal to dynamic width (CSS fluid layout)

From Dev

ExtJS 4.2.1 - border layout with dynamic height

From Dev

TableviewCell dynamic height - auto layout issues

From Dev

Android, dynamic Listview layout changing on scroll

From Java

Equal height rows in CSS Grid Layout

From Dev

Height of rows to fill column - Bootstrap layout

From Dev

Table layout odd and even rows

From Dev

How to set dynamic height for rich:extendedDataTable rows?

From Dev

Dynamic width and fixed height of rows in JPanel

From Dev

UISwitch in CustomCell is not changing the value at specific indexpath

From Dev

dynamic table layout table row content is not working

From Dev

Dynamic number of columns and rows in a responsive layout

From Dev

Firefox Won't Fill Height for Table Layout

From Dev

Changing layout in table row using CSS only

Related Related

HotTag

Archive