ios – 我的表视图在滚动时在SWIFT中重用所选单元格

前端之家收集整理的这篇文章主要介绍了ios – 我的表视图在滚动时在SWIFT中重用所选单元格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
早上好

我的问题是,当我向下滚动向上时,我的表视图重用所选单元格.我的意思是当我从上向下选择一个单元格然后向下滚动时,我选择的一些单元格被选中,也有一些选中的单元格是当我再次向上滚动时没有显示选中,当发生这种情况时我再次选择苹果不仅仅是一个必须不被允许的单元格..我想提一下,我试图从’didSelectRowAtIndexPath’和我保存旧的索引路径在’CellForRowAtIndexPath’中检查它,但它不起作用:

  1. if (old == indexpath)
  2. {
  3. cell?.backgroundColor = UIColor.redColor()
  4. cell?.textLabel?.backgroundColor = UIColor.whiteColor() //to change only the thumbnail color and keep the cell color
  5. }
  6.  
  7. else {
  8. cell?.backgroundColor = UIColor.whiteColor()
  9. }

现在这是我的代码

  1. func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  2. var cell = myTable.dequeueReusableCellWithIdentifier("WorkCell") as UITableViewCell
  3. cell.tag = (indexPath.row) + 1
  4. cell.textLabel?.text = Berufs[indexPath.row]
  5. var img = UIImageView(frame: CGRect(x: 10,y: 3,width: 40,height: 40))
  6. cell.selectionStyle = UITableViewCellSelectionStyle.None
  7. img.image = UIImage(named: "transparent.png")
  8. cell.indentationLevel = 1
  9. cell.indentationWidth = 45
  10. cell.addSubview(img)
  11. return cell
  12. }
  13.  
  14. var old = 1000
  15.  
  16. func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath)
  17. {
  18. let indexPath = tableView.indexPathForSelectedRow()
  19. let cell = tableView.cellForRowAtIndexPath(indexPath!)
  20. var tmpCell = view.viewWithTag(old)
  21. var rowNum = (cell?.tag)! - 1
  22.  
  23. if (cell?.backgroundColor == UIColor.redColor())
  24. {
  25. cell?.backgroundColor = UIColor.whiteColor()
  26. }
  27. else if (cell?.backgroundColor != UIColor.redColor())
  28. {
  29. tmpCell?.backgroundColor = UIColor.whiteColor()
  30. cell?.backgroundColor = UIColor.redColor()
  31. cell?.textLabel?.backgroundColor = UIColor.whiteColor()
  32.  
  33. println("du inteRSSiert dich für \(Berufs[rowNum])")
  34. }
  35.  
  36. old = rowNum + 1
  37.  
  38. }

解决方法

您当前将选择状态存储在backgroundColor中.这不是一个好主意,因为出于性能原因,tableView将重用单元格.

您应该将选定的indexPath保存在数据模型中.如果要允许多个选择,可以将选定的indexPath存储在NSMutableSet中.

  1. override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  2. let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as UITableViewCell
  3. cell.selectionStyle = .None
  4. configure(cell,forRowAtIndexPath: indexPath)
  5. return cell
  6. }
  7.  
  8. var selectedIndexPaths = NSMutableSet()
  9.  
  10. func configure(cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
  11. cell.textLabel!.text = "Row \(indexPath.row)"
  12. if selectedIndexPaths.containsObject(indexPath) {
  13. // selected
  14. cell.backgroundColor = UIColor.redColor()
  15. }
  16. else {
  17. // not selected
  18. cell.backgroundColor = UIColor.whiteColor()
  19. }
  20. }
  21.  
  22.  
  23. override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
  24. if selectedIndexPaths.containsObject(indexPath) {
  25. // deselect
  26. selectedIndexPaths.removeObject(indexPath)
  27. }
  28. else {
  29. // select
  30. selectedIndexPaths.addObject(indexPath)
  31. }
  32. let cell = tableView.cellForRowAtIndexPath(indexPath)!
  33. configure(cell,forRowAtIndexPath: indexPath)
  34. }

如果您只需要单个选择,则应将选定的indexPath保存在NSIndexPath中?实例变量

  1. override func tableView(tableView: UITableView,forRowAtIndexPath: indexPath)
  2. return cell
  3. }
  4.  
  5. var selectedIndexPath: NSIndexPath?
  6.  
  7. func configure(cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
  8. cell.textLabel!.text = "Row \(indexPath.row)"
  9. if selectedIndexPath == indexPath {
  10. // selected
  11. cell.backgroundColor = UIColor.redColor()
  12. }
  13. else {
  14. // not selected
  15. cell.backgroundColor = UIColor.whiteColor()
  16. }
  17. }
  18.  
  19. override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
  20. if selectedIndexPath == indexPath {
  21. // selected same cell -> deselect all
  22. selectedIndexPath = nil
  23. }
  24. else {
  25. // select different cell
  26. let oldSelectedIndexPath = selectedIndexPath
  27. selectedIndexPath = indexPath
  28.  
  29. // refresh old cell to clear old selection indicators
  30. var prevIoUsSelectedCell: UITableViewCell?
  31. if let prevIoUsSelectedIndexPath = oldSelectedIndexPath {
  32. if let prevIoUsSelectedCell = tableView.cellForRowAtIndexPath(prevIoUsSelectedIndexPath) {
  33. configure(prevIoUsSelectedCell,forRowAtIndexPath: prevIoUsSelectedIndexPath)
  34. }
  35. }
  36. }
  37. let selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
  38. configure(selectedCell,forRowAtIndexPath: indexPath)
  39. }

猜你在找的iOS相关文章