在UITableViewHeaderFooterView中重叠文本

时间:2016-01-20 11:29:02

标签: ios objective-c xcode6 xcode7

这里我提到我写的代码,在UITableViewHeaderFooterView滚动时,第二次该方法调用文本重叠(滚动那么多时间文本正在添加),这里我提到屏幕截图:{{3} }

 _imgViewUser.imageURL   = [NSURL URLWithString:modelData.user_image_big];
    _lblUserName.text       = modelData.posted_by;
    _lblTime.text           = modelData.posted_time;

    if ([modelData.commentArray count])
    {
        _imgViewUser.imageURL = [NSURL URLWithString:[[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_user_small"]];
        NSString *nameStr = @"";
        if ([GETVALUE(kUSERID) isEqualToString:[[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_userid"]]) {
            nameStr = @"You";
        }else {
            nameStr = [[modelData.commentArray objectAtIndex:0] valueForKey:@"full_name"];
        }
        _lblUserName.text     = nameStr;
        _lblTime.text         = [[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_post_time"];
    }


    NSString *OriginalFrontendText    = [[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_text"];
    UIFont *font                      = [UIFont fontWithName:@"Arial" size:13.0];
    CGFloat descriptionHeight         = [OriginalFrontendText boundingRectWithSize:CGSizeMake(180, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil].size.height;

    [[self viewWithTag:tag]removeFromSuperview];

    STTweetUserName *commentLbl;
    if ([modelData.comments_count intValue] > 1) {
        commentLbl   = [[STTweetUserName alloc]initWithFrame:CGRectMake(0, 39, self.frame.size.width-10, descriptionHeight) withFont:9.0 attributeTextColor:[UIColor darkGrayColor] name:@"See More"];
       commentLbl.text = [NSString stringWithFormat:@"%@See More",[[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_text"]];
    }else
    {
        commentLbl   = [[STTweetUserName alloc]initWithFrame:CGRectMake(0, 39, self.frame.size.width-10, descriptionHeight) withFont:9.0 attributeTextColor:[UIColor darkGrayColor] name:nil];
        commentLbl.text              = [[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_text"];
    }
    commentLbl.tag = tag;

    if (commentLbl.tag == tag) {
        [commentLbl removeFromSuperview];
    }
    [self addSubview:commentLbl];

    commentLbl.detectionBlock = ^(STTweetHotUserName hotWord, NSString *string, NSString *protocol, NSRange range){
        showAlert(string, nil, @"OK", nil);
        //[self SeeMoreClicked];
    };
    self.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, 150);

i am getting overlopping while scrolling

1 个答案:

答案 0 :(得分:0)

使用以下代码制作页脚视图

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

   UIView * v = [[UIView alloc] init];
   v.frame = CGRectMake(0, 0, tableView.frame.size.width, 44);

   UILabel * label = [[UILabel alloc] init];
   label.frame = CGRectMake(20, 0, tableView.frame.size.width/2, 44);
   label.text = @"Some text";
   [v addSubview:label];

   UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
   btn.frame = CGRectMake(label.frame.size.width+label.frame.origin.x, 0, tableView.frame.size.width/2, 44);
  [btn addTarget:self action:@selector(seeMore) forControlEvents:UIControlEventTouchUpInside];
  btn.titleLabel.text = @"See more";
  [v addSubview:btn];

   return v;
}

根据需要自定义上面的代码。这不会让您的页脚视图与文本重叠。

使用

获取NSString文字的宽度
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
    CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
    size = CGSizeMake(frame.size.width, frame.size.height+20.0f);
}
return size;
}