Selected button in cell is showing up on reuse cells - also using sender.tag for button distinction

拟墨画扇 提交于 2020-01-07 06:57:11

问题


I have a bunch of rows in a table and each row consists of 6 UIButtons (they are checkbox images). I started with just trying to get one button to work before linking all of them and that worked somewhat. The code would update the image to a checked box if I clicked on it and if I scrolled down and back up the first cell first box would be the only one checked. If I repeatedly scroll up and down though, it will randomly unselect that first cell first box. Here is my ViewController code:

//
//  TestViewController.swift
//

import UIKit
import Parse



class TestViewController: UIViewController, UITableViewDelegate {

@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 10

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! protoCell

    configure(cell, forRowAtIndexPath: indexPath)

    cell.customIndexPath = indexPath

    return cell

}



func configure(cell: protoCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if cell.selectedIndexPaths.containsObject(indexPath) {

        println("Contains")
        let image = UIImage(named: "checkedbox.png")
        cell.button.setImage(image, forState: .Normal)
        println(cell.selectedIndexPaths)

    } else {

        println("Not Contains")
        let image = UIImage(named: "checkbox.png")
        cell.button.setImage(image, forState: .Normal)

    }



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

I am controlling the table using a custom cocoa class for my UITableViewCell which has the code here:

//
//  protoCell.swift
//  ParseStarterProject
//

import UIKit

class protoCell: UITableViewCell {

var selectedIndexPaths = NSMutableSet()

var customIndexPath: NSIndexPath = NSIndexPath()

var isPressed = [1, 1, 1, 1, 1, 1]

@IBOutlet var button: UIButton!

@IBAction func buttonPressed(sender: AnyObject) {

    println(isPressed[sender.tag])

    if isPressed[sender.tag] == 1 {

        isPressed[sender.tag] = 2
        let image = UIImage(named: "checkedbox.png")
        sender.setImage(image, forState: .Normal)
        selectedIndexPaths.addObject(customIndexPath)

    } else {

        isPressed[sender.tag] = 1
        let image = UIImage(named: "checkbox.png")
        sender.setImage(image, forState: .Normal)
        selectedIndexPaths.removeObject(customIndexPath)

    }

    println(isPressed)

}

}

I know the code is wrong because of what I am seeing. Also all images besides the first button are repeating. I am not too familiar with Objective-C so some of the searching has been hard.

Any help would be great!


回答1:


Please see the skeleton code below (I just typed here, and not tested) that should give your a fair idea as to what needs be done-

 class Config{
      enum ButtonState : Int {
        case NoneSelected,
        case Button1Selected,
        case Button2Selected,
        case Button3Selected,
        case Button4Selected,
        case Button5Selected,
        case Button6Selected
      }
       var buttonState : Int = . NoneSelected
    }

//Within TableView controller, maintain an array of 10 such objects
var configs = [ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState(), ButtonState()]

//This is how you need to configure the cell now-
func configure(cell: protoCell, forRowAtIndexPath indexPath: NSIndexPath) {

  //First deselect all buttons
   DeselectAllButtons()

  //Get proper config object
   var config = configs[indexPath.row]
   //Maintain a ref to the config in the cell.
   self.config = config

   switch config. buttonState{
   case . NoneSelected
    DeselectAllButtons()
   case let x where (x & Button1Selected) == true
     SelectButton(button1)
      case let x where (x & Button1Selected) == true
     SelectButton(button1)
   case let x where (x & Button2Selected) == true
     SelectButton(button3)
   case let x where (x & Button3Selected) == true
     SelectButton(button3)
   case let x where (x & Button4Selected) == true
     SelectButton(button4)
   case let x where (x & Button5Selected) == true
     SelectButton(button5)
   case let x where (x & Button1Selected) == true
     SelectButton(button6)
   default:
     DeselectAllButtons()
  }
} 

 //And your action in cell can be-
func buttonPressed(sender: AnyObject){
    switch sender{
      case button1
      self.config.buttonState  |= .Button1Selected
      case button2
      self.config.buttonState  |= .Button2Selected
      case button3
      self.config.buttonState  |= .Button3Selected
      case button4
      self.config.buttonState  |= .Button4Selected
      case button5
      self.config.buttonState  |= .Button5Selected
      case button6
      self.config.buttonState  |= .Button6Selected
    }

}


来源:https://stackoverflow.com/questions/32085737/selected-button-in-cell-is-showing-up-on-reuse-cells-also-using-sender-tag-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!