代码之家  ›  专栏  ›  技术社区  ›  Emre Önder

带无限滚动的滚动视图中的uiTableView

  •  1
  • Emre Önder  · 技术社区  · 6 年前

    我有一个由两个元素组成的屏幕( uiview uitableview )。当用户滚动屏幕时,我希望 uiview go out of screen(top)and uitableview start to scroll.我为 uiview提供固定高度,为 uitableviewcells提供固定高度。此外,我正在设置底部和顶部锚定,但如果我不给 uitableview指定特定高度,则 uitableview 如果我给一个 uiTableView的特定高度,它只显示一些单元格而不是全部。

    另外:首先TableView是空的,因为它的数据源是空的。当用户在uiview内点击一个按钮时,datasource完成和tableview刷新数据。

    我有一些帖子,但所有的帖子都说,给合适的视角提供固定的高度。我不知道如何显示固定高度的所有单元格。 如: 滚动视图中的动态高度表视图

    锚定:

    boxview.anchor(self.view.topanchor,left:self.view.leftanchor,bottom:nil,right:self.view.rightanchor,topconstant:20,leftconstant:10,bottomconstant:0,rightconstant:10,widthconstant:0,heightconstant:240)
    stableView.anchor(self.boxView.bottomAnchor,左:self.view.leftAnchor,下:self.scrollView.bottomAnchor,右:self.view.rightAnchor,上常量:0,左常量:0,下常量:0,右常量:0,宽度常量:0,高度常量:100)
    

    How I declare UITableView:

    let stableview=uitableview()。
    var数据源:【支持的服务】?
    
    让ScrollView=uiScrollView()。
    
    viewDidLoad()。{
    stableView.delegate=自己
    stableView.datasource=自身
    stableView.register(basecell.self,forcellReuseIdentifier:“支持的服务sell”)。
    stableView.tableFooterView=uiView()。
    stableView.isscrollenabled=错误
    self.scrollview.addSubview(stableView)
    
    self.view.addSubview(滚动视图)
    }
    @objc func按钮点击(u sender:uibutton){
    //在此执行一些网络请求(数据为temp)
    数据源=数据
    stableView.reloaddata()。
    }
    

    TableView函数:

    func tableview(u tableview:uitableview,numberofrowspiagon section:int)->int{
    如果让data=datasource{
    返回data.count
    }其他{
    返回0
    }
    }
    
    func tableview(u tableview:uitableview,cellforrowat indexpath:indexpath)->uitableviewcell{
    让cell=tableview.dequeuerusablecell(withIdentifier:“SupportedServicesCell”,for:indexPath)作为!基电池
    guard let data=datasource else返回单元格
    cell.textLabel?.text=data[indexpath.row].标题
    
    返回单元格
    }
    
    
    

    在滚动之前我想要它的方式:

    滚动后:

    UITableView)。当用户滚动屏幕时,我希望视图离开屏幕(顶部)并uiTable视图开始滚动。我给你一个固定的高度视图固定高度UITableViewCells'的。此外,我正在设置底部和顶部锚,但是uiTable视图如果我不给一个特定的高度uiTable视图.如果我给一个uiTable视图,它只显示一些单元格,而不是全部。

    另外:首先TableView是空的,因为它的数据源是空的。当用户在uiview中点击一个按钮时,datasource完成并刷新tableview数据。

    我有一些帖子,但所有的帖子都说,给合适的视角提供固定的高度。我不知道如何显示固定高度的所有单元格。 如: Dynamic height TableView in a Scroll View

    锚定:

    boxView.anchor(self.view.topAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 20, leftConstant: 10, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 240)
    sTableView.anchor(self.boxView.bottomAnchor, left: self.view.leftAnchor, bottom: self.scrollView.bottomAnchor, right: self.view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 100)
    

    如何声明UITableView:

    let sTableView = UITableView()
    var dataSource: [SupportedService]?
    
    let scrollView = UIScrollView()
    
    viewDidLoad(){
    sTableView.delegate = self
    sTableView.dataSource = self
    sTableView.register(BaseCell.self, forCellReuseIdentifier:"supportedServicesCell")
    sTableView.tableFooterView = UIView()
    sTableView.isScrollEnabled = false
    self.scrollView.addSubview(sTableView)
    
    self.view.addSubview(scrollView)
    }
    @objc func buttonTapped(_ sender: UIButton){
    //Do Some Network Requests Here (Data is temp)
    dataSource = DATA
    sTableView.reloadData()
    }
    

    TableView函数:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let data = dataSource{
            return data.count
        }else{
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "supportedServicesCell", for: indexPath) as! BaseCell
        guard let data = dataSource else { return cell }
        cell.textLabel?.text = data[indexPath.row].title
    
        return cell
    }
    

    滚动前我想要它的方式:

    滚动后:

    2 回复  |  直到 5 年前
        1
  •  1
  •   Rakesha Shastri    5 年前

    • tableView
    • view
        2
  •  0
  •   Y_Y    6 年前