如何将选定的tableview行存储在数组中? [英] how to store selected tableview rows in an array?

查看:33
本文介绍了如何将选定的tableview行存储在数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有比我的方式更好的选择行的方法(也欢迎选择行).否则我只需要知道如何从 tableview 存储选定的行,当点击完成按钮(栏项目)

I know there is better way to select a row then my way( which one welcome too). otherwise else I only need to know how to store selected rows from tableview ,when click Done button(bar item)

注意:在我的 tableview 中,我有多个部分,如果我从一个部分中选择一行,其他部分的行将被取消选择.

Note: in my tableview, I have multiple sections and if I select one row from one section, other section's rows will be unselected.

更多说明,假设有 3 个部分,每个部分有 5 行.

for more clarification, suppose there are 3 sections, each section has 5 rows.

step1 : 我在 section1 中选择 row1 和 row2

step1 : I select row1 and row2 in section1

step2:如果我从第 2 节或第 3 节中选择任何行,则第 1 节的所有行都将被取消选择.

step2: if I select any row from section 2 or section 3, section1's all row deselected.

第 3 步:假设用户从第 3 部分中选择第 3 行和第 5 行,然后按下完成按钮

step 3: suppose user select row3 and row 5 from section 3 and then press done button

第 4 步:现在我想将行的值(名称)存储在一个数组中.

step 4: now I want to store row's value (name) in an array.

我的代码如下,

extension TradeSelectionController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if isSearching && searchController.searchBar.text != "" {
            return viewModel.filteredCategories?.count ?? 0
        }else{
            return viewModel.categorySkills.count
        }
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if isSearching && searchController.searchBar.text != ""{
            
            return viewModel.filteredCategories?[section].skill.count ?? 0
            
        }else{
            let section = viewModel.categorySkills[section]
            return section.skill.count
        }
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "TradeSelectionTableViewCell")
        
        if ((cell != nil)) {
            
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "TradeSelectionTableViewCell")
        }
        
        if isSearching && searchController.searchBar.text != "" {
            
            cell?.textLabel?.textColor = UIColor.lightGray
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 10)
            cell?.textLabel?.text = "(\(String(describing: viewModel.categorySkills[indexPath.section].vName)))"
            
            let text =
                viewModel.filteredCategories?[indexPath.section].skill[indexPath.row].vName
            
            //don't change order , change text size
            cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 16)
            cell?.detailTextLabel?.text = text
            
            
        }else{
            cell?.textLabel?.text = viewModel.categorySkills[indexPath.section].skill[indexPath.row].vName
            
            
        }
        if selectedIngredients.contains(indexPath){
            cell?.accessoryType = .checkmark
            cell?.backgroundColor = UIColor.lightGray
        }else{
            cell?.accessoryType = .none
            cell?.backgroundColor = .none
        }
        
        return cell!
    }
    
        override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
            if isSearching && searchController.searchBar.text != ""{
                return viewModel.filteredCategories?[section].vName
            }else{
                let titleText = viewModel.categorySkills[section].vName
                return titleText
            }
        }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        lastSelectedIndexPath = indexPath.section
        if self.selectedIngredients.contains(indexPath){
            self.selectedIngredients.remove(indexPath)
            totalSkills[indexPath.row] = false
        }else{
            self.selectedIngredients.insert(indexPath)
            totalSkills[indexPath.row] = true
            
        }
        self.tableView.reloadData()
        
    }
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        print(selectedIngredients)
        if lastSelectedIndexPath != nil {
            if lastSelectedIndexPath != indexPath.section {
                selectedIngredients.removeAll()
                print(selectedIngredients)
            }
        }
        
        return indexPath
    }
    //    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    //        return 30
    //    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
        
        
        //   headerView.addRoundCorner(10)
        headerView.backgroundColor = .lightGray
        let label = UILabel()
        label.frame = CGRect.init(x: 10, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
        label.text = viewModel.categorySkills[section].vName
        label.font = ThemeFonts.font(with: 16, for: .bold)
        label.textColor = ThemeColors.primaryTextColor
        headerView.addSubview(label)
        return headerView
    }
}

我尝试过如下所示,但不起作用....

I have tried like below, but is not working....

   @objc private func doneAction(_ sender: Any?) {
        
        var selectedSkills = [Skill]()
        if let selectedIndex = tableView.indexPathsForSelectedRows {
            selectedIndex.forEach({
                //viewModel.categorySkills[indexPath.section].skill[indexPath.row].vName
                //let skill = viewModel.skills[$0.row]
                let skill = filteredObjects[$0.row]
                print(skill)
                //           selectedSkills.append(skill)
            })
        }

        dismiss(animated: true, completion: nil)
    }

推荐答案

向表示数据模型中单元格的结构添加选定标志.(对于分段表视图,结构的二维数组可以很好地用作数据模型.)

Add a selected flag to the struct that represents cells in your data model.(For sectioned table view a 2-dimensional array of structs works well as a data model.)

在响应用户选择单元格的代码中维护该标志.

Maintain that flag in your code that responds to the user selecting cells.

这篇关于如何将选定的tableview行存储在数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆