2014-02-07 253 views
0

我需要创建并填充自定义单元格。这些细胞的高度大约是300-400。UITableview单元格高度计算不正确,单元格重叠

所以我有一个populateCellView函数,我调用heightForRowAtIndexPath函数来计算高度,并在cellForRowAtIndexPath将该视图添加到单元格内容视图,但有时单元格有时重叠。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return [self populateCellView:indexPath.row].frame.size.height; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UIView *populateCell= nil; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     populateCell=[[UIView alloc] initWithFrame:CGRectZero]; 
     [[populateCell layer] setBorderWidth:2.0f]; 


     populateCell= [self populateCellView:indexPath.row]; 
     [[cell contentView] addSubview:populateCell]; 

    } 

    if (!populateCell){ 
     populateCell = (UIView*)[cell viewWithTag:1]; 
     populateCell= [self populateCellView:indexPath.row]; 

    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 

} 

填入视图功能:

-(UIView *)populateCellView:(int)forCase 
{ 

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,500)]; 
    [view setBackgroundColor:[UIColor colorWithRed:40/255.0 green:150/255.0 blue:213/255.0 alpha:1.0]]; 
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

    if ([view subviews]){ 
     for (UIView *subview in [view subviews]) { 
      [subview removeFromSuperview]; 
     } 
    } 


    UILabel *caseNumberLabel; 
    UILabel *caseNameLabel; 
    UILabel *caseSummaryLabel; 
    UILabel *caseAncLabel; 


    UILabel *caseNumberText; 
    UILabel *caseNameText; 
    UILabel *caseSummaryText; 
    UILabel *caseAncText; 



    //static label for case number 
    caseNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,10,250, 50)]; 
    caseNumberLabel.textColor = [UIColor whiteColor]; 
    caseNumberLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f]; 
    caseNumberLabel.text [email protected]"Case Number:"; 
    caseNumberLabel.backgroundColor = [UIColor clearColor]; 
    caseNumberLabel.numberOfLines=0; 
    caseNumberLabel.adjustsFontSizeToFitWidth = NO; 
    caseNumberLabel.tag=forCase; 
    caseNumberLabel.frame=[self calculateLabelFrame:caseNumberLabel]; 

    //case number from plist 
    caseNumberText = [[UILabel alloc] initWithFrame:CGRectMake(caseNumberLabel.frame.size.width+50,caseNumberLabel.frame.origin.y,self.view.frame.size.width-caseNumberLabel.frame.size.width-50, 50)]; 
    caseNumberText.textColor = [UIColor whiteColor]; 
    caseNumberText.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24.0f]; 
    caseNumberText.text =[[self agenda] getCaseNumber:forCase]; 
    caseNumberText.backgroundColor = [UIColor clearColor]; 
    caseNumberText.numberOfLines=0; 
    caseNumberText.adjustsFontSizeToFitWidth = NO; 
    caseNumberText.tag=forCase; 
    //[caseNumberLabel sizeToFit]; 
    caseNumberText.frame=[self calculateLabelFrame:caseNumberText]; 




    //static label for case name 
    caseNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseNumberText.frame.origin.y + caseNumberText.bounds.size.height+5,250, 50)]; 
    caseNameLabel.textColor =[UIColor whiteColor]; 
    caseNameLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f]; 
    caseNameLabel.text [email protected]"Case Name:"; 
    caseNameLabel.backgroundColor = [UIColor clearColor]; 
    caseNameLabel.numberOfLines=0; 
    caseNameLabel.adjustsFontSizeToFitWidth = NO; 
    caseNameLabel.tag=forCase; 
    //[caseNumberLabel sizeToFit]; 
    caseNameLabel.frame=[self calculateLabelFrame:caseNameLabel]; 

    //case name from plist 
    caseNameText = [[UILabel alloc] initWithFrame:CGRectMake(caseNameLabel.frame.size.width+50,caseNameLabel.frame.origin.y,self.view.frame.size.width-caseNameLabel.frame.size.width-50, 50)]; 
    caseNameText.textColor = [UIColor whiteColor]; 
    caseNameText.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24.0f]; 
    caseNameText.text =[[self agenda] getCaseName:forCase]; 
    caseNameText.backgroundColor = [UIColor clearColor]; 
    caseNameText.numberOfLines=0; 
    //caseNameText.adjustsFontSizeToFitWidth = NO; 
    caseNameText.tag=forCase; 
    [caseNameText sizeToFit]; 
    //caseNameText.lineBreakMode = NSLineBreakByWordWrapping; 
    caseNameText.frame=[self calCellLabelFrame:caseNameText previousLabel:caseNameLabel]; 




    //static label for case summary 
    caseSummaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseNameText.frame.origin.y + caseNameText.bounds.size.height+20,250, 50)]; 
    caseSummaryLabel.textColor = [UIColor whiteColor]; 
    caseSummaryLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f]; 
    caseSummaryLabel.text [email protected]"Case Summary:"; 
    caseSummaryLabel.backgroundColor = [UIColor clearColor]; 
    caseSummaryLabel.numberOfLines=0; 
    caseSummaryLabel.adjustsFontSizeToFitWidth = NO; 
    caseSummaryLabel.tag=forCase; 

    //[caseSummaryLabel sizeToFit]; 
    caseSummaryLabel.frame=[self calculateLabelFrame:caseSummaryLabel]; 

    //case name from plist 
    caseSummaryText = [[UILabel alloc] initWithFrame:CGRectMake(caseSummaryLabel.frame.size.width+50,caseSummaryLabel.frame.origin.y,self.view.frame.size.width-caseSummaryLabel.frame.size.width-100, 50)]; 
    caseSummaryText.textColor = [UIColor whiteColor]; 
    caseSummaryText.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24.0f]; 
    caseSummaryText.text =[[self agenda] getCaseSummary:forCase]; 
    caseSummaryText.backgroundColor = [UIColor clearColor]; 
    caseSummaryText.numberOfLines=0; 
    //caseSummaryText.adjustsFontSizeToFitWidth = NO; 
    caseSummaryText.tag=forCase; 
    [caseSummaryText sizeToFit]; 
    //caseSummaryText.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    caseSummaryText.frame=[self calCellLabelFrame:caseSummaryText previousLabel:caseSummaryLabel]; 



    //static label for anc 
    caseAncLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseSummaryText.frame.origin.y + caseSummaryText.bounds.size.height+15,250, 50)]; 
    caseAncLabel.textColor = [UIColor whiteColor]; 
    caseAncLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f]; 
    caseAncLabel.text [email protected]"ANC:"; 
    caseAncLabel.backgroundColor = [UIColor clearColor]; 
    caseAncLabel.numberOfLines=0; 
    caseAncLabel.adjustsFontSizeToFitWidth = NO; 
    caseAncLabel.tag=forCase; 
    //[ccaseAncLabel sizeToFit]; 
    caseAncLabel.frame=[self calculateLabelFrame:caseAncLabel]; 

    //case name from plist 
    caseAncText = [[UILabel alloc] initWithFrame:CGRectMake(caseAncLabel.frame.size.width+50,caseAncLabel.frame.origin.y,self.view.frame.size.width-caseAncLabel.frame.size.width-50, 50)]; 
    caseAncText.textColor = [UIColor whiteColor]; 
    caseAncText.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24.0f]; 
    caseAncText.text =[[self agenda] getCaseAnc:forCase]; 
    caseAncText.backgroundColor = [UIColor clearColor]; 
    caseAncText.numberOfLines=0; 
    //caseAncText.adjustsFontSizeToFitWidth = NO; 
    caseAncText.tag=forCase; 
    [caseAncText sizeToFit]; 
    //caseSummaryText.lineBreakMode = NSLineBreakByWordWrapping; 
    caseAncText.frame=[self calCellLabelFrame:caseAncText previousLabel:caseAncLabel]; 


    //add button 
    UIButton *acceptButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [acceptButton setTag:forCase]; 
    acceptButton.titleLabel.tag=1; 
    [acceptButton setFrame:CGRectMake(caseAncLabel.frame.size.width+50,caseAncText.frame.origin.y + caseAncText.bounds.size.height+20,181,39)]; 
    NSString *[email protected]"view_attachment.png"; 
    UIImage *acceptbuttonImage = [UIImage imageNamed:radioButtonImage]; 
    [acceptButton setBackgroundImage:acceptbuttonImage forState:UIControlStateNormal]; 
    [acceptButton addTarget:self action:@selector(showAttachements:) forControlEvents:UIControlEventTouchUpInside]; 



    //whie line 
    UIView *anotherline = [[UIView alloc] initWithFrame:CGRectMake(0, acceptButton.frame.origin.y + acceptButton.bounds.size.height+15, self.view.frame.size.width, 1)]; 
    anotherline.backgroundColor=[UIColor whiteColor]; 
    anotherline.tag=forCase; 


    [view addSubview:acceptButton]; 

    [view addSubview: caseNumberLabel]; 
    [view addSubview:caseNumberText]; 

    [view addSubview:caseNameLabel]; 
    [view addSubview:caseNameText]; 

    [view addSubview:caseSummaryLabel]; 
    [view addSubview:caseSummaryText]; 

    [view addSubview:caseAncText]; 
    [view addSubview:caseAncLabel]; 


    [view addSubview:anotherline]; 
    view.frame = CGRectMake(0,0, self.view.frame.size.width,anotherline.frame.origin.y + anotherline.bounds.size.height+5); 

    CGFloat redLevel = rand()/(float) RAND_MAX; 
    CGFloat greenLevel = rand()/(float) RAND_MAX; 
    CGFloat blueLevel = rand()/(float) RAND_MAX; 

    view.backgroundColor = [UIColor colorWithRed: redLevel 
              green: greenLevel 
               blue: blueLevel 
              alpha: 1.0]; 



    return view; 


} 

我这样调用

dispatch_async(dispatch_get_main_queue(), ^{ 
    [_agenda loadFromPlist]; 
    [[self tableView] reloadData]; 
}); 

任何想法,为什么细胞重叠重装数据?

回答

0

,是你的UITableView cellforIndexPath造成重叠

试试这个

if (!populateCell){ 
    populateCell = (UIView*)[cell viewWithTag:1]; 
    populateCell= [self populateCellView:indexPath.row]; 

    if ([[cell contentView] subviews]){ 
     for (UIView *subview in [[cell contentView] subviews]) { 
      [subview removeFromSuperview]; 
     } 
    } 
    [[cell contentView] addSubview:populateCell]; 


} 
cell.backgroundColor=[UIColor clearColor]; 
0

在UITableViewDataSource方法的末尾,tableView:cellForRowAtIndexPath:,您必须致电[[cell contentView] addSubView:populateCell]。如果表成功取出单元格,则必须检索相应的populateCell并将其添加到contentView。

0

如果你用故事板,并设置了不同高度的细胞,并在tableview中的行,高度通常会重叠。其他答案也是非常有效的,但通常当我遇到这个问题时,错误在于你的故事板中tableview对象的行高和单元高度的不同值...