单元格中的选定按钮显示在重复使用的单元格上 - 也使用sender.tag进行按钮区分

时间:2015-08-19 02:47:09

标签: ios xcode swift uitableview xcode6

我在表中有一堆行,每行包含6个UIButtons(它们是复选框图像)。我开始只是尝试在连接所有这些按钮之前让一个按钮工作,这在某种程度上起作用。如果我点击它,代码会将图像更新为一个复选框,如果我向下滚动并备份第一个单元格,则第一个框将是唯一一个选中的框。如果我反复向上和向下滚动,它将随机取消选择第一个单元格第一个框。这是我的ViewController代码:

//
//  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.
}


}

我正在使用我的UITableViewCell的自定义cocoa类来控制表,其中包含以下代码:

//
//  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)

}

}

我知道代码是错误的,因为我所看到的。此外,除第一个按钮外的所有图像都在重复。我对Objective-C不太熟悉,所以有些搜索很难。

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

请参阅下面的骨架代码(我只是在这里输入,而不是测试),这应该让您对需要做什么有充分的了解 -

 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
    }

}