使用 swift 中的按钮选择和取消选择表格视图单元格时出现问题

问题描述 投票:0回答:1

代码:在此代码中,我已经在

selectedRows
中选择了单元格ID,一旦我到达vc,这些单元格就会显示为选中状态,如果我选择并取消选择单元格,那么
getting correct values in array selectedRows and getting wrong names in selectedNamesArray
为什么?最初在数组中添加选定的名称时,名称索引也不匹配。阿米哪里错了?

class ViewController2: UIViewController {
@IBOutlet weak var tableView: UITableView!

var selectedRows = [32, 38, 89, 11]
var selectedNamesArray: [String] = []

var arrayModel: [SampleModel] = [SampleModel(id: 32, name: "Apple"),
                                 SampleModel(id: 38, name: "Smasung"),
                                 SampleModel(id: 22, name: "Black berry"),
                                 SampleModel(id: 76, name: "Nokia"),
                                 SampleModel(id: 12, name: "Real me"),
                                 SampleModel(id: 75, name: "Lenova"),
                                 SampleModel(id: 36, name: "Pocco"),
                                 SampleModel(id: 11, name: "Motorola"),
                                 SampleModel(id: 37, name: "Infinix"),
                                 SampleModel(id: 88, name: "China"),
                                 SampleModel(id: 89, name: "Jio")
]

override func viewDidLoad() {
    super.viewDidLoad()
    
    if !selectedRows.isEmpty{
        selectedNamesArray = arrayModel
            .filter { selectedRows.contains($0.id) }
            .map { $0.name }
    }
    print("existing names: \(selectedNamesArray) \n and ids: \(selectedRows)")
}
}

extension ViewController2: UITableViewDelegate, UITableViewDataSource{

func numberOfSections(in tableView: UITableView) -> Int {
    2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return arrayModel.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableviewCell1", for: indexPath) as? TestTableviewCell1
    cell?.selectionStyle = .none
    cell?.titleLbl?.text = arrayModel[indexPath.row].name
    cell?.checkBtn.tag = indexPath.row
    
    cell?.isChecked = selectedRows.contains(arrayModel[indexPath.row].id)
    
    cell?.buttonAction = { [self] sender in
        
        if selectedRows.contains(arrayModel[indexPath.row].id) {
            if let indexToRemove = selectedRows.firstIndex(of: arrayModel[indexPath.row].id) {
                
                selectedRows.remove(at: indexToRemove)
                
                if indexToRemove < selectedNamesArray.count {
                    selectedNamesArray.remove(at: indexToRemove) // Remove corresponding name
                }
                //   selectedNamesArray.remove(at: indexToRemove) // Remove the corresponding name
                cell?.isChecked = false
            }
        } else {
            selectedRows.append(arrayModel[indexPath.row].id)
            selectedNamesArray.append(arrayModel[indexPath.row].name) // Add the corresponding name
            cell?.isChecked = true
        }
        print("Selected rows: \(selectedRows)")
        print("Selected names: \(selectedNamesArray)")
    }
    return cell!
}
}

class TestTableviewCell1: UITableViewCell{

@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var checkBtn: UIButton!
var isChecked = false{
    didSet{
        checkBtn.backgroundColor = isChecked ? .blue : .green
    }
}
var buttonAction: ((_ sender: AnyObject) -> Void)?
@IBAction func buttonPressed(_ sender: UIButton) {
    self.buttonAction?(sender)
}
}

struct SampleModel{
var id: Int
var name: String
}

o/p:

现有名称:[“Apple”、“Smasung”、“Motorola”、“Jio”] and ids: [32, 38, 89, 11] //这里索引也与两个数组值都不匹配

进行一些选择之后

所选行:[32, 38, 89, 36] 所选名称:[“Apple”、“Smasung”、“Motorola”、“Pocco”] //agin 索引不匹配

arrays swift uitableview button multipleselection
1个回答
-1
投票

这是更新的代码,解决您遇到的问题,附上屏幕截图以供参考..只需替换此代码并运行:)

cell?.buttonAction = { [self] sender in
                
                if selectedRows.contains(arrayModel[indexPath.row].id) {
                    if let indexToRemove = selectedRows.firstIndex(of: arrayModel[indexPath.row].id) {
                        
                        let name = arrayModel[indexPath.row].name
                        if selectedNamesArray.contains(name) {
                            
                            if let indexofName = selectedNamesArray.firstIndex(of: name) {
                                selectedNamesArray.remove(at: indexofName)
                            }
                        }
                        
                        selectedRows.remove(at: indexToRemove)
                        cell?.isChecked = false
                    }
                } else {
                    selectedRows.append(arrayModel[indexPath.row].id)
                    selectedNamesArray.append(arrayModel[indexPath.row].name) // Add the corresponding name
                    cell?.isChecked = true
                }
                print("Selected rows: \(selectedRows)")
                print("Selected names: \(selectedNamesArray)")
            }

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.