ios - 选中和取消选中 TableView Controller 中的按钮

标签 ios swift uitableview swift4

我是 ios swift4 的新手,我只是在 TableView Controller 中创建选中和取消选中按钮,我的任务是使用选中按钮选择表中的项目,我想取消选中单击取消选中按钮时选中的项目。帮助我来解决这个问题

最佳答案

daysRemainderTableView 中使用名为 daysSelectionimageView 的 imageview。这是在 tableviewcell 中选择和取消选择行的示例。

     @IBOutlet weak var daysRemainderTableView: UITableView!

         var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

var selectedDaysArray = [Int]() 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return days.count
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "RemainderDaysCell", for: indexPath)as! RemainderDaysCell
                cell.remainderDaysLabel.text = "\(days[indexPath.row])"


                if selectedDaysArray.contains(indexPath.row){
                    cell.daysSelectionimageView.image = #imageLiteral(resourceName: "selectedImage")
                }else{
                    cell.daysSelectionimageView.image = #imageLiteral(resourceName: "ring")
                }
                return cell
            }

            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                if let cell = tableView.cellForRow(at: indexPath) as? RemainderDaysCell
                {
                    if selectedDaysArray.isEmpty{
                        self.selectedDaysArray = [Int]()
                    }
                    if selectedDaysArray.contains(indexPath.row){
                        if let indexElement = selectedDaysArray.index(of: indexPath.row){
                            cell.daysSelectionimageView.image = #imageLiteral(resourceName: "ring")
                            selectedDaysArray.remove(at: indexElement)
                        }
                    }else{
                        cell.daysSelectionimageView.image = #imageLiteral(resourceName: "selectedImage")
                        selectedDaysArray.append(indexPath.row)
                    }
                }
            }

我正在indexpath和didSelectRow ai indexpath处的cellforrow中检查selectedDaysArray,如果selectedDaysArray已选择索引,则我将删除元素,如果不是,则将indexemement附加到selectedDaysArray。

关于ios - 选中和取消选中 TableView Controller 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533637/

相关文章:

iPhone 导航栏

ios - 我如何可以动画化 UINavigationBar 而不是 UITabBar 的条形色调颜色的变化?

ios - 如何使 UITableView 的节标题完全透明

ios - 滚动时重新排序 UITableViewCells

ios - Unicode不间断空格在标签末尾被删除

ios - 从 NSSet 中获取 NSManagedObject

ios - 如何知道触摸了哪个tableview单元格中的哪张图片?

xcode - 从另一个类swift调用函数

ios - WKWebView 中的嵌入推文有时无法完全加载

ios - Spotify 集成因 openURL Swift 失败