【问题标题】:Table view cell scrolling issue表格视图单元格滚动问题
【发布时间】:2021-11-18 19:26:25
【问题描述】:

我有一个表格视图和集合视图来显示图像。垂直滚动的表格视图和水平滚动的集合视图。图像显示为设备的全屏。但是当我垂直滚动时,显示前一个单元格的一半,显示下一个单元格的一半。我使用 Table view constant is 0 来超级查看所有 4 个约束。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return myTableView.frame.size.height;
}

返回单元格的高度,但即使这样也会导致同样的问题。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.myTableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    return cell
}

此表格视图单元格包含集合视图

class TableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    
 func setUpCollectionView(){
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
    collectionView?.register(VideoCollectionViewCell.self, forCellWithReuseIdentifier: VideoCollectionViewCell.identifier)
    collectionView?.isPagingEnabled = true
    collectionView?.dataSource = self
    collectionView?.delegate = self
    self.contentView.addSubview(collectionView!)
    collectionView?.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        collectionView!.topAnchor.constraint(equalTo: contentView.topAnchor),
        collectionView!.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        collectionView!.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        collectionView!.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
    ])
}

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: self.frame.width, height: self.frame.height)
    }
}

这是我的实现,我不希望图像显示为一半/一半。我正在尝试全屏显示。谁能告诉我为什么会这样?

【问题讨论】:

  • 能否提供 gif 或图片以便我们更好地理解问题?
  • 有一张来自 url 的图片,和我从我的设备上传的一样。它填充 imageView 的图像。
  • 查看这个答案,希望对您有所帮助:stackoverflow.com/a/68622400/11974184

标签: ios swift uitableview uitableviewautomaticdimension


【解决方案1】:

您只需要 UICollectionView 显示列表图像并在 UICollectionViewFlowLayout 中设置属性滚动水平

 myCollectionView.dataSource = self
    myCollectionView.delegate = self
    myCollectionView.contentMode = .scaleAspectFill
   //  scroll the images page by page, set property isPagingEnabled is true
    myCollectionView.isPagingEnabled = true
    
    let layout = UICollectionViewFlowLayout()
    // set sroll horizontal here
    layout.scrollDirection = .horizontal
    myCollectionView.collectionViewLayout = layout

【讨论】:

  • 我已将我的配置更新为收藏视图。可以看看吗?
  • 如果你想在设备上显示图片并水平滚动全屏,你只需要UICollectionView,不要在UITableView里面使用UICollectionView
  • 我只是在表格视图中遇到垂直滚动问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多