【问题标题】:Populating dynamic images in UITableViewCell在 UITableViewCell 中填充动态图像
【发布时间】:2015-10-26 15:29:22
【问题描述】:

我正在尝试制作一个与 Instagram 完全一样的带有匿名提要的应用。我使用 AFNetworking 下载 JSON 数据,使用 SDWebImage 添加和缓存图像。

当我加载可变大小的图像时,我希望表格视图单元格自动调整其高度。

现在的问题是,当应用程序加载时,图像无法正确显示,但在我向下滚动并滚动回同一个单元格后,图像会按照我想要的方式自行调整,但图像上方和下方总是有一个空格.

滚动前


回滚后


以下是 UIImage 视图的约束:

  1. superview 的尾随空格:0
  2. 领先空间到superview : 0
  3. 顶部空间:8
  4. 底部空间:8
  5. 固有大小设置为占位符,内容模式设置为 Aspect Fit。

这是我的 ViewDidLoad 方法代码:

 override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.barTintColor = UIColor.yellowColor()

    feedList = NSMutableArray() 

    self.feedTable.rowHeight = UITableViewAutomaticDimension
    self.feedTable.estimatedRowHeight = 200.0

    var connection :  CommunicationManager! = CommunicationManager()

    //initiate a connection and load feed data
    connection.getFeedData( { (response, error) -> Void in

        if(response != nil){
            dispatch_async(dispatch_get_main_queue(), {

                self.feedList = response
                self.feedTable.hidden = false
                self.feedTable.reloadData()
            })

        }

}

TableView 的 CellForRowAtIndex:

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FeedTableViewCell


    let feedData : NSDictionary! = feedList[indexPath.row] as! NSDictionary

    let imgURL : NSURL! = NSURL(string: feedData.valueForKey("image_name") as! String)
    cell.feedImage.sd_setImageWithURL(imgURL, placeholderImage: UIImage(named: "LoadingImage"))

    return cell
}

我尽了最大努力解决这个问题,但无法让它发挥作用。谁能告诉我如何使用故事板解决这个问题。 提前致谢。

【问题讨论】:

    标签: ios swift uitableview autolayout sdwebimage


    【解决方案1】:

    您需要调用 layoutIfneeded 方法:使用以下代码:-

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    
    [cell.nImageView sd_setImageWithURL:[NSURL URLWithString:[imageUrlArray objectAtIndex:indexPath.row]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        cell.widthConst.constant = image.size.width;
        [cell layoutIfNeeded];
        [cell layoutSubviews];
    
    }];
    
    return cell;
    

    }

    如果你想要动态宽度,那么你需要添加固定宽度约束并在块中更新它。

    参考截图

    【讨论】:

    • 感谢您的回答。你能解释一下什么是 cell.widthConst.constant 因为我没有得到任何关于 widthConst 的自动建议
    • widthConst 是 imageview 的恒定宽度约束。正如我所说,如果你想设置图像视图的宽度等于下载图像的宽度,那么你可以使用这个约束出口来修改宽度
    • 当我将尾随和前导空格等约束添加为 0 时,会自动计算宽度对吗?
    • 如果你正确检查不同尺寸的图像,你会得到一些奇怪的宽度输出。你可以检查一下。它在我的情况下不起作用,所以我添加了这个约束。我认为由于这些 tableview 属性,单元格高度会再次计算,但没有更新图像宽度的规定。试试吧,让我知道它是否适用于您的情况。
    • 我按你说的添加了一个宽度约束,但它现在给出一条错误消息“可能以下列表中的至少一个约束是你不想要的”
    猜你喜欢
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多