gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:08:10 25 4
gpt4 key购买 nike

我的表中有一堆行,每行由 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 不太熟悉,所以有些搜索很困难。

任何帮助都会很棒!

最佳答案

请参阅下面的框架代码(我刚刚在此处输入,未经测试),它应该可以让您清楚地了解需要做什么 -

 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
}

}

关于ios - 单元格中选定的按钮显示在重用单元格上 - 还使用 sender.tag 进行按钮区分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com