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
}
滚动前我想要它的方式:
滚动后: