问题描述
我在一个表格中有一堆行,每行包含 6 个 UIButton(它们是复选框图像).我一开始只是尝试让一个按钮工作,然后再将所有按钮链接起来,这有点奏效.如果我点击它,代码会将图像更新为选中的框,如果我向下滚动并备份第一个单元格,第一个框将是唯一选中的框.如果我反复上下滚动,它会随机取消选择第一个单元格的第一个框.这是我的 ViewController 代码:
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.
}
}
我正在为我的 UITableViewCell 使用自定义可可类来控制表格,其代码如下:
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)
}
}
我知道代码是错误的,因为我所看到的.此外,除了第一个按钮之外的所有图像都在重复.我对 Objective-C 不太熟悉,所以有些搜索很难.
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.
任何帮助都会很棒!
推荐答案
请查看下面的框架代码(我只是在此处输入,未测试),它应该可以让您对需要完成的工作有一个公平的想法-
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
}
}
这篇关于单元格中的选定按钮显示在重用单元格上 - 还使用 sender.tag 区分按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!