开发者社区> 问答> 正文

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

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

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];                
            });
        });
    }
}

展开
收起
a123456678 2016-07-20 15:31:56 2345 0
1 条回答
写回答
取消 提交回答
  • - (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;
            });
        });
    }
    }
    2019-07-17 19:58:57
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载