0

I'm kind of new to iOS and the Issue i have right now is in the below screen shot. I have eight labels in each UITableviewCell which is a prototype cell. When the table loads first time it looks like this

enter image description here

But when I scroll down and up it loads the whole UIlabels text as shown below

enter image description here

I'm manually assigning the coordinates based on the nearest top UILabel's height. I have tried to reload table, reload sections etc in viewDidAppear but no use as all the data is present before tableview datasource methods are called.

 prototypecell * cell = [self.CHTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if(!cell)
        {
            cell = [[prototypecell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        history * hist = [history objectAtIndex:(NSInteger)indexPath.row ];
        [cell CreateFromHistory:hist];


        return cell;

in HeightForRowAtIndexPath method

 prototypecell * cell = [self.CHTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        [cell CreateFromHistory:[History objectAtIndex:indexPath.row]];

        return cell.height;

The createfromhistory methods sets all the uilabels text that are present inside init.

CreateFromHistory Method

 NSInteger XOriginLbl = 20;
NSInteger XOriginValue = 134;


UIFont * font = [UIFont systemFontOfSize:17];
NSDictionary *attributes = @{NSFontAttributeName: font};


CGSize size = [citation.date sizeWithAttributes:attributes];
[Date setFrame:CGRectMake(XOriginValue, 4, Date.frame.size.width, [citation.date sizeWithAttributes:attributes].height)];
[self.dateLBL setFrame:CGRectMake(XOriginLbl, 4, self.dateLBL.frame.size.width, [citation.date sizeWithAttributes:attributes].height)];
Date.text = citation.date;
self.dateLBL.text = @"Date";

Violation.numberOfLines = 0;
Violation.lineBreakMode = NSLineBreakByWordWrapping;
size = [citation.violation sizeWithAttributes:attributes];
[Violation setFrame:CGRectMake(XOriginValue, Date.frame.origin.y + Date.frame.size.height + 2, Violation.frame.size.width, size.height)];
[self.violationLBL setFrame:CGRectMake(XOriginLbl, Date.frame.origin.y + Date.frame.size.height + 2, self.violationLBL.frame.size.width, size.height)];
self.violationLBL.text = @"Violation:";
Violation.text = citation.violation;


Where.numberOfLines = 0;
Where.lineBreakMode = NSLineBreakByWordWrapping;
Where.text = citation.location;
[Where sizeToFit];
size = [citation.location sizeWithAttributes:attributes];
CGRect size2 = Where.frame;
[Where setFrame:CGRectMake(XOriginValue, Violation.frame.origin.y + Violation.frame.size.height + 2, Where.frame.size.width,size2.size.height)];
[self.whereLBL setFrame:CGRectMake(XOriginLbl, Violation.frame.origin.y + Violation.frame.size.height + 2, self.whereLBL.frame.size.width, size.height)];
self.whereLBL.text = @"Where:";



size = [citation.fine sizeWithAttributes:attributes];
[FineAmount setFrame:CGRectMake(XOriginValue, Where.frame.origin.y + Where.frame.size.height + 2, FineAmount.frame.size.width, size.height)];
[self.fineLBL setFrame:CGRectMake(XOriginLbl, Where.frame.origin.y + Where.frame.size.height + 2, self.fineLBL.frame.size.width, size.height)];
self.fineLBL.text = @"Fine:";
FineAmount.text = citation.fine;


Comments.numberOfLines = 2;
Comments.lineBreakMode = NSLineBreakByWordWrapping;
size = [citation.comments sizeWithAttributes:attributes];
[Comments setFrame:CGRectMake(XOriginValue, FineAmount.frame.origin.y + FineAmount.frame.size.height + 2, Comments.frame.size.width, size.height)];
[self.commentLBL setFrame:CGRectMake(XOriginLbl, FineAmount.frame.origin.y + FineAmount.frame.size.height + 2, self.commentLBL.frame.size.width, size.height)];
self.commentLBL.text = @"Comments";
Comments.text =  citation.comments;
self.height = Comments.frame.origin.y + size.height + 2;

Update: Tried to reload each sections/ each row separately, but Haven't receive positive result.

Edit and Final Update (Found work around solution) UILabels using constraints excellent solution

Community
  • 1
  • 1
Mahesh
  • 29
  • 1
  • 7

0 Answers0