类似微博列表,每一个UITableViewCell有一个图片,当下拉的时候,会有图片重叠.怎么去掉重叠的呢?

发布于 2022-08-25 11:37:39 字数 2316 浏览 22 评论 0

类似微博列表,每一个UITableViewCell有一个图片,当下拉的时候,会有图片重叠.怎么去掉重叠的呢?请看图

TableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * ID = @"WeiboCustomCellIdentifier";

    [tableView registerClass:[WeiboCustomCell class] forCellReuseIdentifier:ID];
    WeiboCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    MStatusItem *status = [_data_array objectAtIndex:indexPath.row];    
    [cell.screenNameLabel setText:status.user_screen_name];
    [cell.timeLabel setText:status.pubtime_str];
    [cell.weiboTextLabel setText:status.getPlainText];
    cell.userAvatarUrl = status.user_proimg;
    if(status.hasImage){
        cell.weiboImageUrl = status.tnpic;
    }else{
        cell.weiboImageUrl = @"";
        cell.weiboImageView.hidden = YES;
    }    
    return cell;
}

CustomTableViewCell:

- (void)setWeiboImageUrl:(NSString *)weiboImageUrl{
    if ([weiboImageUrl isEqualToString:@""] == NO && weiboImageUrl != nil){
        NSURL *imageURL = [NSURL URLWithString: weiboImageUrl];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
            dispatch_async(dispatch_get_main_queue(), ^{
                CGSize textSize = [self.weiboTextLabel.text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.frame.size.width - 10.0 * 3 - 60.0, 2000.0f)];
                float height = textSize.height + 20.0 * 3;
                NSLog(@"Height:%f", height);                
                self.weiboImageView = [[UIImageView alloc] initWithFrame: CGRectMake(65.0, height, 100, 124)];               
                UIImage *image = [[UIImage alloc]initWithData:imageData];
                self.weiboImageView.image = image;
                self.weiboImageView.contentMode = UIViewContentModeScaleAspectFit;
                self.userInteractionEnabled = NO;                                
                [self.contentView addSubview:self.weiboImageView];                
            });
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2022-09-01 11:37:39
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString * ID = @"WeiboCustomCellIdentifier";

WeiboCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (nil == cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
MStatusItem *status = [_data_array objectAtIndex:indexPath.row];
[cell.screenNameLabel setText:status.user_screen_name];
[cell.timeLabel setText:status.pubtime_str];
[cell.weiboTextLabel setText:status.getPlainText];
cell.userAvatarUrl = status.user_proimg;
if(status.hasImage){
    cell.weiboImageUrl = status.tnpic;
}else{
    cell.weiboImageUrl = @"";
    cell.weiboImageView.hidden = YES;
}
return cell;

}

- (void)setWeiboImageUrl:(NSString *)weiboImageUrl{
if ([weiboImageUrl isEqualToString:@""] == NO && weiboImageUrl != nil){
    NSURL *imageURL = [NSURL URLWithString: weiboImageUrl];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        dispatch_async(dispatch_get_main_queue(), ^{
            CGSize textSize = [self.weiboTextLabel.text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.frame.size.width - 10.0 * 3 - 60.0, 2000.0f)];
            float height = textSize.height + 20.0 * 3;
            NSLog(@"Height:%f", height);
            self.weiboImageView = [[UIImageView alloc] initWithFrame: CGRectMake(65.0, height, 100, 124)];
            if (nil == self.weiboImageView) {
                self.weiboImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
                [self.contentView addSubview:self.weiboImageView];
            }
            self.weiboImageView = [[UIImageView alloc] initWithFrame: CGRectMake(65.0, height, 100, 124)];
            UIImage *image = [[UIImage alloc]initWithData:imageData];
            self.weiboImageView.image = image;
            self.weiboImageView.contentMode = UIViewContentModeScaleAspectFit;
            self.userInteractionEnabled = NO;
        });
    });
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文