Swift:避免滚动前 2 个表格视图单元格
Swift: Avoid scrolling top 2 tableview cells
有没有办法停止在表格视图中滚动某些单元格?我希望 UITableView 中的前 2 个单元格是静态的,并在其他单元格上启用滚动。启用滚动 属性 在 tableview 上,因此它会滚动所有单元格。
前 2 个单元格的高度均为 44.0。我尝试了下面的代码,但它仍然滚动前 2 个单元格。
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isScrolling = true
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let startScrollingOffset = 88.0
if (scrollView.contentOffset.y < CGFloat(startScrollingOffset)) {
// moved to top
isScrollDown = false
} else if (scrollView.contentOffset.y > CGFloat(startScrollingOffset)) {
// moved to bottom
isScrollDown = true
} else {
// didn't move
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if isScrollDown == false {
return
}
}
如果您想要实现像列表滚动时保持在顶部的静态单元格这样的效果,请使用 header view 属性 of UITableView
,这里有一个示例使这项工作所需的最少代码。将 headerView 替换为您不想滚动的任何单元格。
class VC: UITableViewController {
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = .purple
return headerView
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = String(describing: indexPath)
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
}
解决方案可能是从您的数据源(可能是数组)中取出前两行数据,然后使用这两行数据创建自定义视图。然后使用 viewForHeaderInSection
在 tableviewHeader 中设置自定义视图
由于您已经删除了前两行,因此您的 table 将显示第三个条目的数据,而前两个将显示在它们的顶部 header。
有没有办法停止在表格视图中滚动某些单元格?我希望 UITableView 中的前 2 个单元格是静态的,并在其他单元格上启用滚动。启用滚动 属性 在 tableview 上,因此它会滚动所有单元格。
前 2 个单元格的高度均为 44.0。我尝试了下面的代码,但它仍然滚动前 2 个单元格。
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isScrolling = true
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let startScrollingOffset = 88.0
if (scrollView.contentOffset.y < CGFloat(startScrollingOffset)) {
// moved to top
isScrollDown = false
} else if (scrollView.contentOffset.y > CGFloat(startScrollingOffset)) {
// moved to bottom
isScrollDown = true
} else {
// didn't move
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if isScrollDown == false {
return
}
}
如果您想要实现像列表滚动时保持在顶部的静态单元格这样的效果,请使用 header view 属性 of UITableView
,这里有一个示例使这项工作所需的最少代码。将 headerView 替换为您不想滚动的任何单元格。
class VC: UITableViewController {
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = .purple
return headerView
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = String(describing: indexPath)
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
}
解决方案可能是从您的数据源(可能是数组)中取出前两行数据,然后使用这两行数据创建自定义视图。然后使用 viewForHeaderInSection
由于您已经删除了前两行,因此您的 table 将显示第三个条目的数据,而前两个将显示在它们的顶部 header。