代码之家  ›  专栏  ›  技术社区  ›  emenegro

使用insets将视图添加到UITableView,它有什么问题吗?

  •  0
  • emenegro  · 技术社区  · 6 年前

    我正在一个视图的底部添加一个视图 UITableView UICollextionView )但不是作为单元格或页脚,而是将其添加到表视图本身(因此,添加到 UIScrollView )然后玩 contentInset

    footer

    以下是代码摘录:

    let yCoord = collectionView.contentSize.height >= view.frame.height - viewHeight
        ? collectionView.contentSize.height
        : view.frame.height - viewHeight
    
    let bottomView = UIView(frame: CGRect(x: 0, y: yCoord, width: collectionView.contentSize.width, height: viewHeight))
    
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: viewHeight, right: 0)
    collectionView.addSubview(bottomView)
    

    我想知道这种方法是否会被认为是有害的,例如,铝的问题,旋转,重新装载的桌子。。。我不知道这是什么,事实上这是一个非常类似于iOS6之前的“拉到刷新”功能,但我更确定的是,在将其投入生产之前。

    如果这可能被认为是一个问题,除了添加一个新的单元之外,您还建议什么解决方案?

    3 回复  |  直到 6 年前
        1
  •  1
  •   Sairam    6 年前

    如果您想要以上功能,您必须在scrollView中使用tableView和bottom view,并使用content observer根据内容为tableView提供动态高度,并将bottom视图保持在scrollView的底部,将tableView作为top-to-bottom视图,并将greaterThan或equal作为属性。

    希望它能起作用。

        2
  •  1
  •   Yogita J    6 年前

    页脚视图清除颜色,并在页脚视图中放置另一个视图 有底部、左侧、右侧和高度限制

    @IBOutlet weak var footerView: UIView!
    
       //MARK:- View Controller life cycle method
    override func viewDidLoad() {
        super.viewDidLoad()
        yourTableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        yourTableView.removeObserver(self, forKeyPath: "contentSize")
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if(keyPath == "contentSize"){
                let contentHeight: CGFloat = yourTableView.contentSize.height
                let tableHeight = yourTableView.frame.size.height
    
                if contentHeight < tableHeight {
                    footerView.frame = CGRect(x: footerView.frame.origin.x, y: footerView.frame.origin.y, width: footerView.frame.size.width, height: CGFloat(tableHeight - contentHeight + 44.0))
                    // 44.0 is your initial footer height which you put in xib
    
                } else {
                    footerView.frame = CGRect(x: footerView.frame.origin.x, y: footerView.frame.origin.y, width: footerView.frame.size.width, height: 44.0)
                     // 44.0 is your initial footer height which you put in xib
                }
        }
    }
    
        3
  •  0
  •   GaétanZ    6 年前

    scrollViewDidScroll 事件,它将在用户每次滚动时更新页脚视图框架。顺便说一句,我肯定会使用约束来放置我的页脚视图,而不是使用可能会更改的框架。

    你的执行中有什么有害的东西吗?

    我们应该把这些代码放在哪里?

    我们KVO是否应该观察表视图框架及其内容,以便在每次更改页脚视图时更新它们的框架?

    viewDidLayoutLayouts 并确保不要多次添加页脚视图。 ViewDidLayouts布局