How to programmatically set CustomCell table height?

I am making a UITableview that contains CustomCell. My CustomCell contains a UITextView and some other controls, but I want to set My cell height to 300, if my UITextViewtext contains some text, and if my TextView text contains Blank text, then not text, then I want the cell height 170 to be possible. I am writing code for this, but it does not work.

My code is kind of like

NSString *magazineDescription=[self.firstArray valueForKey:@"long_description"];
if([magazineDescription isKindOfClass:[NSString class]])
{
    cellOne.magazineDescriptionTextView.text = magazineDescription;
    cellOne.frame=CGRectMake(0, 0, 300, 300);
}
else
{
    cellOne.magazineDescriptionTextView.text =  @"";
    cellOne.magazineDescriptionTextView.hidden=TRUE;
    cellOne.frame=CGRectMake(0, 0, 300, 170);
}

Here, my self.firstArray contains the JSON data value, and sometimes my keyValue @ "long_description" is zero, so I want to hide the TextView From Cell and Set height of Cell from 300 to 170, and the width should remain the same for both cases.

I know a method for this as:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

, . , .

+4
3
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Your text";
    UIFont *cellFont = [UIFont systemFontOfSize:YourFontSize];
    CGSize constraintSize = CGSizeMake(TextviewWidth, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"labelSize : %f", labelSize.height);
    return labelSize.height;
}
+3
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Fetch your string from array as below or according to your code
    NSString *magazineDescription=[self.firstArray valueForKey:@"long_description"];
    if(magazineDescription!=nil)
    {
        return 300.0f;
    }
    else
    {
        return 170.0f;
    }
}
0

First declare CustomerCell property

@property (strong, nonatomic) CustomerCell *cell;

in the table ViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([@"" isEqualToString:_cell.textView.text] || _cell.textView.text == nil) {
        return 170;
    } else {
        return 300;
    }
}

hope this helps

0
source

All Articles