UITableviewCell在滚动后加载UILabel数据

时间:2014-03-06 03:13:36

标签: ios uitableview uilabel

我是 iOS 的新手,我现在的问题是在下面的屏幕截图中。我在每个UITableviewCell中有八个标签,这是一个原型单元格。当表第一次加载时,它看起来像这样

enter image description here

但是当我向下滚动时,它会加载整个UIlabel文本,如下所示

enter image description here

我是根据最近的UILabel高度手动分配坐标。 我试图在viewDidAppear中重新加载表,重新加载部分等但没有用,因为在调用tableview数据源方法之前所有数据都存在。

 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;
HeightForRowAtIndexPath方法

中的

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

        return cell.height;

createfromhistory方法设置init中存在的所有uilabels文本。

CreateFromHistory方法

 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;

更新: 试图分别重新加载每个部分/每一行,但没有收到正面结果。

编辑和最终更新(找到解决方案) UILabels using constraints excellent solution

0 个答案:

没有答案