滚动iOS UITableView看不到视觉错误(Scrolling of iOS UITableView out of view visual errors)

编程入门 行业动态 更新时间:2024-10-26 13:35:38
滚动iOS UITableView看不到视觉错误(Scrolling of iOS UITableView out of view visual errors)

菜鸟。 我有一个锁定按钮,可以将值锁定到UITableView中的表格单元格中。 单击时,更新基础模型(isLocked boolean),然后将图像从打开的锁切换到闭合锁并更改背景颜色。

一切正常。 除了从视图中滚动一长串单元格并重新进入之外。我得到的单元格不正确,或者有时会丢失新的bg颜色和图形。

我确定我需要在某些时候更新UITableView? 我只是不知道如何或何时。 一直在尝试tableView.reloadData()和.reloadRows(at:...在锁定函数中没有运气。

这里有一些代码...表视图设置

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // do a whole lot of stuff and then set the lock button tag cell.lockButton.tag = indexPath.row // Return cell for display return cell }

锁定功能

func slideLockFunc(sender: UIButton, peopleData: inout [PersonData], peopleTableView: UITableView) { // when lock is clicked change the peopleData lock value to locked and the graphic to locked if let cell: PersonTableCell = ((peopleTableView.cellForRow(at: IndexPath(row:sender.tag, section: 0))) as? PersonTableCell) { if peopleData[sender.tag].isLocked == false { peopleData[sender.tag].isLocked = true cell.lockButton.setImage(UIImage(named: "icon-locked"), for: .normal) cell.cellBackground.backgroundColor = UIColor(red: 0.995, green: 0.924, blue: 0.924, alpha: 1) } else { peopleData[sender.tag].isLocked = false cell.lockButton.setImage(UIImage(named: "unlock"), for: .normal) cell.cellBackground.backgroundColor = UIColor.white } }

有帮助吗? 谢谢

NOOB. I have a lock button to lock values into a table cell in a UITableView. On click, the underlying model is updated (isLocked boolean) and I switch the image to a closed lock from an open lock and change the background color.

All works fine. Except when scrolling a long list of cells out of the view and back in. I get incorrect cells displaying or sometimes losing the new bg color and graphic.

I'm sure I'm needing to update the UITableView at some point? I just don't know how or when. Have been trying tableView.reloadData() and .reloadRows(at: ... in the locking function without luck.

some code here... Table View setup

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // do a whole lot of stuff and then set the lock button tag cell.lockButton.tag = indexPath.row // Return cell for display return cell }

lock function

func slideLockFunc(sender: UIButton, peopleData: inout [PersonData], peopleTableView: UITableView) { // when lock is clicked change the peopleData lock value to locked and the graphic to locked if let cell: PersonTableCell = ((peopleTableView.cellForRow(at: IndexPath(row:sender.tag, section: 0))) as? PersonTableCell) { if peopleData[sender.tag].isLocked == false { peopleData[sender.tag].isLocked = true cell.lockButton.setImage(UIImage(named: "icon-locked"), for: .normal) cell.cellBackground.backgroundColor = UIColor(red: 0.995, green: 0.924, blue: 0.924, alpha: 1) } else { peopleData[sender.tag].isLocked = false cell.lockButton.setImage(UIImage(named: "unlock"), for: .normal) cell.cellBackground.backgroundColor = UIColor.white } }

Any help? THANKS

最满意答案

永远不要在cellForRowAt之外更改单元格的属性更改您需要更改数据源数组,然后需要重新加载tableView的行。 而不是使用tag尝试获取indexPath使用convert(_:to:)获取tapped的位置和indexPathForRow(at:)与该位置获取该单元格的indexPath 。

func slideLockFunc(sender: UIButton, peopleData: inout [PersonData], peopleTableView: UITableView) { let point = sender.superview?.convert(sender.center, to: peopleTableView) ?? CGPoint.zero if let indexPath = peopleTableView.indexPathForRow(at: point) { peopleData[indexPath.row].isLocked = !peopleData[indexPath.row].isLocked peopleTableView.reloadRows(at: [indexPath], with: .automatic) } }

现在使用你的lockButton在界面构建器或单元格的awakeFromNib中设置这个default/normal图像和selected的按钮状态,这样你每次只需选择和取消选择按钮就不需要改变它。

class PersonTableCell: UITableViewCell { @IBOutlet var lockButton: UIButton! override func awakeFromNib() { lockButton.setImage(UIImage(named: "unlock"), for: .normal) lockButton.setImage(UIImage(named: "icon-locked"), for: .selected) } }

现在在cellForRowAt方法中,您只需要检查自定义对象的isLocked属性。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! PersonTableCell //Now use isLocked property to change the lockButton state cell.lockButton.isSelected = peopleData[indexPath.row].isLocked cell.cellBackground.backgroundColor = peopleData[indexPath.row].isLocked ? UIColor(red: 0.995, green: 0.924, blue: 0.924, alpha: 1) : UIColor.white //your other code return cell }

Never make cell's property changes outside the cellForRowAt you need to just change your data source array and then need to reload the tableView's row. Also instead of working with tag try to get indexPath using convert(_:to:) to get location of tapped and the indexPathForRow(at:) with that location to get indexPath of that cell's.

func slideLockFunc(sender: UIButton, peopleData: inout [PersonData], peopleTableView: UITableView) { let point = sender.superview?.convert(sender.center, to: peopleTableView) ?? CGPoint.zero if let indexPath = peopleTableView.indexPathForRow(at: point) { peopleData[indexPath.row].isLocked = !peopleData[indexPath.row].isLocked peopleTableView.reloadRows(at: [indexPath], with: .automatic) } }

Now with your lockButton either in interface builder or in awakeFromNib of cell set this both image with default/normal and selected state of button so you don't need to change it every time need to simply select and deselect the button.

class PersonTableCell: UITableViewCell { @IBOutlet var lockButton: UIButton! override func awakeFromNib() { lockButton.setImage(UIImage(named: "unlock"), for: .normal) lockButton.setImage(UIImage(named: "icon-locked"), for: .selected) } }

Now in cellForRowAt method you just need to check for isLocked property of your custom object.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! PersonTableCell //Now use isLocked property to change the lockButton state cell.lockButton.isSelected = peopleData[indexPath.row].isLocked cell.cellBackground.backgroundColor = peopleData[indexPath.row].isLocked ? UIColor(red: 0.995, green: 0.924, blue: 0.924, alpha: 1) : UIColor.white //your other code return cell }

更多推荐

本文发布于:2023-08-06 03:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视觉   错误   UITableView   iOS   errors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!