单元格中的选定按钮显示在重用单元格上 - 还使用 sender.tag 区分按钮
Selected button in cell is showing up on reuse cells - also using sender.tag for button distinction
我在 table 中有一堆行,每行包含 6 个 UIButton(它们是复选框图像)。我开始只是尝试让一个按钮工作,然后再链接所有按钮,这有点奏效。如果我单击它,代码会将图像更新为一个选中的框,如果我向下滚动并返回第一个单元格,第一个框将是唯一选中的框。如果我反复上下滚动,它会随机取消选择第一个单元格的第一个框。这是我的 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 class 控制 table,这里有代码:
//
// 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 不太熟悉,所以有些搜索很困难。
任何帮助都会很棒!
请查看下面的框架代码(我只是在此处输入,未经测试),它应该让您清楚地知道需要做什么-
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
}
}
我在 table 中有一堆行,每行包含 6 个 UIButton(它们是复选框图像)。我开始只是尝试让一个按钮工作,然后再链接所有按钮,这有点奏效。如果我单击它,代码会将图像更新为一个选中的框,如果我向下滚动并返回第一个单元格,第一个框将是唯一选中的框。如果我反复上下滚动,它会随机取消选择第一个单元格的第一个框。这是我的 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 class 控制 table,这里有代码:
//
// 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 不太熟悉,所以有些搜索很困难。
任何帮助都会很棒!
请查看下面的框架代码(我只是在此处输入,未经测试),它应该让您清楚地知道需要做什么-
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
}
}