user1174864
user1174864

Reputation:

UIlabel gets shrink on scrolling UiTableView

I have two UILabel in side of UICell, which contains dynamic text so i need to resize its frame according to content, for which im using [string sizeWithFont] method to calculate the height of label view's frame inside of TableView heightForRowAtIndexPath to set the height of cell according to label height. Now what the problem is when i scroll my table, label inside of cell starts to shrink if i remove [label sizeToFit] method it doesn't shrink but from that my labels are getting overlapped which looks very messy. Where i am wrong please guide me..

here is my code for cellRowAtIndexPath method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cell");
BOOL ente = FALSE;

static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (CustomCell *) currentObject;
            ente = TRUE;
            break;
        }
    }
}
/*
[cell.title sizeToFit];
[cell.dataTime sizeToFit];

CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

cell.title.frame = CGRectMake(50, 6, size1.width, size1.height);


CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

cell.dataTime.frame = CGRectMake(50, 24, size2.width, size2.height);

*/
cell.title.text = [mainEventArray objectAtIndex:[indexPath row]];
cell.dataTime.text = [mainEventTimeArray objectAtIndex:[indexPath row]];



//if (ente) {
  //  NSLog(@"helllloo");

    [cell.title sizeToFit];
    [cell.dataTime sizeToFit];
//}


return cell;
}

and for heightForRowAtIndexPath method

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


static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

{

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }
}

    CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

    cell.title.frame = CGRectMake(0, 0, size1.width, size1.height);


CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

cell.dataTime.frame = CGRectMake(0, 0, size2.width, size2.height);


//NSLog(@"%f", size1.height + size2.height + 20);

    return size1.height + size2.height + 20;

//NSString *str = [heights_ objectAtIndex:[indexPath row]];

  // return [str floatValue] ;

}

Upvotes: 5

Views: 2950

Answers (4)

Denny
Denny

Reputation: 285

NSString *reuseIdentifier = @"EventTitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
cell=nil;
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ;

}

Upvotes: 0

Deepak Srivastava
Deepak Srivastava

Reputation: 170

In your case you need to set the height of cell based on the label.

Check the link :

http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/

There is a function named

-(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject

Use that function to calculate height as :

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat _height  = [self getHeightForLabel:self.label.text font:[self.label font]];
    return _height;
}

and do the same while creating and adding label in the cell. I hope this will help.

Upvotes: 0

Rushabh
Rushabh

Reputation: 3203

Try This code.

    label.numberOfLines = 0; // allows label to have as many lines as needed
    label.text = @"some long text";
    [label sizeToFit];
    NSLog(@"Label's frame is: %@", NSStringFromCGRect(label.frame));

For reference : click here

Upvotes: -1

Niru Mukund Shah
Niru Mukund Shah

Reputation: 4733

Instead of doing your own customization. Try with dynamic uitableviewcell height. You will find really usefull & comparatively easier than what you have done.

You can check it here : http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ with full of explanation. Enjoy programming.

Upvotes: 3

Related Questions