【发布时间】:2021-05-06 17:29:16
【问题描述】:
我有一个带有 1 个单元格的 UITableView,当加载数组时,我只想让用户看到前 5 行的内容并模糊其余部分。因此,如果有一个包含 20 个项目的数组,则前 5 个需要可见,其余 15 个需要模糊。使用下面的代码,我只能在第 5 行添加模糊,我无法弄清楚。非常感谢任何帮助。
视图控制器
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
cell.cellLbl?.text = array[indexPath.row]
if indexPath.row == 5 {
visualEffectView.frame = cell.bounds
visualEffectView.layer.masksToBounds = true
cell.addSubview(visualEffectView)
}
return cell
}
【问题讨论】:
-
而不是“if indexPath.row == 5”,您是否尝试过“if indexPath.row > 5”??
-
它不起作用,它只模糊 1 行,当我向下滚动时,模糊出现在最后一行。
-
哦,我明白了,但也许你可以给我们一些截图,或者更好的 GIF
-
已添加屏幕截图。
-
太棒了。在答案部分查看我的答案:)
标签: ios swift uitableview cell blur