Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically change height of cells in objective C

I have created an app with a table view. Which uses a view and a label in each cell. But if I create the views and cells in the (!cell) code it returns empty cells and if I remove the (!cell) condition it displays the data but does not take dynamic height. Can anyone please help me.

- (void)viewDidLoad{ 
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", LanguageFile]];

NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:DataPath];

self.reloadArray = [tempDict objectForKey:@"Rows"];}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.reloadArray count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get data for the current row
  NSString *textData = [reloadArray objectAtIndex:indexPath.section]

CGFloat dataTextHeight = [self getLabelHeightForIndex:textData];

    if(dataTextHeight < 44)
    {
        dataTextHeight = 44;
    }

    return dataTextHeight;
}

-(CGFloat)getLabelHeightForIndex:(NSString *)string
{
CGSize maximumSize = CGSizeMake(280, 10000);

CGSize labelHeightSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];

if(labelHeightSize.height < 44){
    labelHeightSize.height = 44;
}

return labelHeightSize.height;
}



-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";

static const int textViewTag = 1, textLabelTag = 2;

UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"standard_back.png"]];

img.frame = tableView.frame;
tableView.backgroundView = img;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    // First view
    UIView *textView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 280.0, 36.00)];
    textView.tag = textViewTag;
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [cell.contentView addSubview:textView];

    // First label
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, 270.0, 36.00)];
    textLabel.tag = textLabelTag;
    textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0f];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.numberOfLines = 0;
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    // textLabel.clipsToBounds = YES;
    [cell.contentView addSubview:textLabel];
}


    NSString *textData = [reloadArray objectAtIndex:(indexPath.section)];

    CGFloat dataTextHeight = [self getLabelHeightForIndex:textData];

    UIView *textView = [cell.contentView viewWithTag:textViewTag];
    CGRect textViewFrame = textView.frame;
    textView.frame = CGRectMake(0.0, 0.0, textViewFrame.size.width, dataTextHeight);

    UILabel *textLabel = [cell.contentView viewWithTag:textLabelTag];
    CGRect textLabelFrame = textLabel.frame;
    textLabel.frame = CGRectMake(10.0, 0.0, textLabelFrame.size.width, dataTextHeight);
    textLabel.text = textData;
    textLabel.backgroundColor= [UIColor clearColor];
    textLabel.textAlignment = NSTextAlignmentCenter;


cell.backgroundColor = [UIColor colorWithWhite:0 alpha:.65];
cell.textLabel.numberOfLines = 0; // Multiline
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

return cell;
}

Thanks in advance.

like image 459
Neha Dangui Avatar asked Jan 13 '23 20:01

Neha Dangui


1 Answers

I saw a lot of solutions but all was wrong or uncomplet. You can solve all problems with 5 lines in viewDidLoad and autolayout. This for objetive C:

_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;

For swift 2.0:

 self.tableView.estimatedRowHeight = 80
 self.tableView.rowHeight = UITableViewAutomaticDimension      
 self.tableView.setNeedsLayout()
 self.tableView.layoutIfNeeded()
 self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

Now create your cell with xib or into tableview in your Storyboard With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"

enter image description here

You can donwload the code in the next url: https://github.com/jposes22/exampleTableCellCustomHeight

References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

like image 168
Jose Pose S Avatar answered Jan 19 '23 10:01

Jose Pose S