【问题标题】:swift: How to set UITableViewCell display fullscreen with InfiniteScrolling (allow paging)swift:如何使用 InfiniteScrolling 设置 UITableViewCell 显示全屏(允许分页)
【发布时间】:2018-02-06 07:50:02
【问题描述】:

我有一个自定义UITableView,具有无限滚动和分页功能。我的每个单元格的背景都有UIImageView,我希望每次向上或向下滚动时都会将每个图像显示为全屏。

我用这个功能全屏了,但是一次无限滚动后我的视图不是全屏的。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return self.view.frame.size.height
}

【问题讨论】:

  • 发布您的完整表格视图方法代码和屏幕截图,供您根据需要进行设计。

标签: ios swift uitableview fullscreen


【解决方案1】:

迟到的答案,可能对其他人有所帮助。 用于 TableViewcell 的全屏分页。请按照以下步骤操作。

(认为没有导航栏)

  1. TableView 约束应该在 allside 中使用 superview(不是 safeArea),常量为 0。
  1. 如下配置您的 Tableview。

    覆盖 func viewDidLoad() {

     super.viewDidLoad()
     configureTableView()
    

    }

    私有函数 configureTableView() {

         tableView.rowHeight = UIScreen.main.bounds.height
         tableView.estimatedRowHeight = UIScreen.main.bounds.height
         tableView.separatorStyle = .none
         tableView.isPagingEnabled = true
         tableView.bounces = false
         tableView.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude
         tableView.sectionHeaderHeight = CGFloat.leastNormalMagnitude
         tableView.estimatedSectionFooterHeight = CGFloat.leastNormalMagnitude
         tableView.sectionFooterHeight = CGFloat.leastNormalMagnitude
         tableView.contentInsetAdjustmentBehavior = .never
         tableView.delegate = self
         tableView.dataSource = self
     }
    

【讨论】:

  • 刚刚添加了您的代码。完美运行。谢谢你的详细回答。
  • 快乐编码@Umair_UAS
【解决方案2】:

请在下面找到源代码,您将在其中获得随着不同图像滚动的表格视图。

斯威夫特 4

    //
    //  ImageTVC.swift
    //
    //  Created by Test User on 06/02/18.
    //  Copyright © 2018. All rights reserved.
    //

    import UIKit

    class ImageTVC: UITableViewCell {

        @IBOutlet weak var imgView: UIImageView!

        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }

    }



    //
    //  TableViewVC.swift
    //
    //  Created by Test User on 06/02/18.
    //  Copyright © 2018. All rights reserved.
    //


    class TableViewVC: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    }

    extension TableViewVC: UITableViewDataSource, UITableViewDelegate {

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 100
        }

        //----------------------------------------------------------------

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageTVC

            if indexPath.item % 2 == 0 {

                cell.imgView?.backgroundColor = UIColor(red: CGFloat(indexPath.item * 2)/255.0, green: CGFloat(indexPath.item * 0)/255.0, blue: CGFloat(indexPath.item * 0)/255.0, alpha: 1.0)

            } else if indexPath.item % 3 == 0 {

                cell.imgView?.backgroundColor = UIColor(red: CGFloat(indexPath.item * 0)/255.0, green: CGFloat(indexPath.item * 2)/255.0, blue: CGFloat(indexPath.item * 0)/255.0, alpha: 1.0)

            } else {

                cell.imgView?.backgroundColor = UIColor(red: CGFloat(indexPath.item * 0)/255.0, green: CGFloat(indexPath.item * 0)/255.0, blue: CGFloat(indexPath.item * 2)/255.0, alpha: 1.0)
            }

            print("IndexPath.row = \(indexPath.row)")

            return cell
        }

        //----------------------------------------------------------------

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return tableView.bounds.size.height
        }
    }

还可以找到故事板设计。

【讨论】:

  • 一开始分页没问题。但是第二次无限滚动后分页不适合
  • 我已经准备了一个演示,我已经在其中进行了测试,在 n 次无限滚动后它可以完美运行。
  • 在我通过 json 从 web 加载的无限数据中。一次输入 10 个数据。
  • 如果您可以分享您的代码,那么我可以为您提供解决方案!
  • 如果您能接受我的回答,我将不胜感激,因为它与您的要求非常相关!
猜你喜欢
  • 2015-04-16
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
相关资源
最近更新 更多